php - mysql query name from list id -


i have little problem here... let me explain..

lets have table this...

barang

╔══════════╦════════════╦════════════╗ ║ idbarang ║    nama    ║ idkategori ║ ╠══════════╬════════════╬════════════╣ ║        1 ║ ban becak  ║ 1,3        ║ ║        2 ║ velg becak ║ 2,4        ║ ╚══════════╩════════════╩════════════╝ 

kategori

╔════════════╦══════════════╗ ║ idkategori ║ namakategori ║ ╠════════════╬══════════════╣ ║          1 ║ ban dalam    ║ ║          2 ║ velg canggih ║ ║          3 ║ ban keren    ║ ║          4 ║ velg monster ║ ╚════════════╩══════════════╝ 

and question is.. how fetch result become this

╔══════════╦════════════╦═══════════════════════════╗ ║ idbarang ║    nama    ║        idkategori         ║ ╠══════════╬════════════╬═══════════════════════════╣ ║        1 ║ ban becak  ║ ban keren,ban dalam       ║ ║        2 ║ velg becak ║ velg monster,velg canggih ║ ╚══════════╩════════════╩═══════════════════════════╝ 

i try use find_in_set gets nothing...

thanks before help...

maybe have missed else when use find_in_set()

select  a.idbarang,         a.nama,         group_concat(b.namakategori) idkategori    barang         inner join kategori b             on find_in_set(b.idkategori, a.idkategori) group   a.idbarang, a.nama 

output

╔══════════╦════════════╦═══════════════════════════╗ ║ idbarang ║    nama    ║        idkategori         ║ ╠══════════╬════════════╬═══════════════════════════╣ ║        1 ║ ban becak  ║ ban keren,ban dalam       ║ ║        2 ║ velg becak ║ velg monster,velg canggih ║ ╚══════════╩════════════╩═══════════════════════════╝ 

the ideal thing normalized table. bad practice store values in comma separated value in single column. if many-to-many relationship, transform current design three-table database.

barang

  • idbarang (pk)
  • name

kategori

  • idkategori (pk)
  • namakategori

barang_kategori

  • idbarang (fk)
  • idkategori (fk)

sample records

barang

╔══════════╦════════════╗ ║ idbarang ║    nama    ║ ╠══════════╬════════════╣ ║        1 ║ ban becak  ║ ║        2 ║ velg becak ║ ╚══════════╩════════════╝ 

kategori

╔════════════╦══════════════╗ ║ idkategori ║ namakategori ║ ╠════════════╬══════════════╣ ║          1 ║ ban dalam    ║ ║          2 ║ velg canggih ║ ║          3 ║ ban keren    ║ ║          4 ║ velg monster ║ ╚════════════╩══════════════╝ 

barang_kategori

╔══════════╦════════════╗ ║ idbarang ║ idkategori ║ ╠══════════╬════════════╣ ║        1 ║          1 ║ ║        1 ║          3 ║ ║        2 ║          2 ║ ║        2 ║          4 ║ ╚══════════╩════════════╝ 

new query

select  a.idbarang,         a.nama,         group_concat(c.namakategori) kategorylista    barang         inner join barang_kategori b             on a.idbarang = b.idbarang         inner join kategori c             on b.idkategori = c.idkategori group   a.idbarang, a.nama 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -