0

tables examples

TABLE: taxonomy

+--+----+
|ID|name|
+--+----+
|1 |demo|
+--+----+

TABLE: settings

+--+-----+-----+-----+
|ID|taxId|title|value|
+--+-----+-----+-----+
|1 |1    |phone|1234 |
+--+-----+-----+-----+
|2 |1    |addr |5678 |
+--+-----+-----+-----+

i want output like

+--+----+-----+----+
|ID|name|phone|addr|
+--+----+-----+----+
|1 |demo|1234 |5678|
+--+----+-----+----+

I tried

SELECT 
    t.id
    ,t.name
    ,s.value AS s.title 
FROM
    taxonomy t, 
    settings s
WHERE 
    s.taxId = t.id

But sure thing it gave me error. Plus I want to loop through all rows of settings to set s.value as s.title :(

  • So is it possible to aggregate all rows from settings table and convert row to use title field as a column header and its value = value field?
  • Is this a good way to do this?
  • Is there anyway to design my table?

The reason I use such a structure is that / I don't know how many settings I will be needing so I don't want to restrict my self in a single flat table structure.

Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
Zalaboza
  • 8,899
  • 16
  • 77
  • 142

0 Answers0