Reputation: 63
I currently have two database tables:
DB1
+-----------------------------+
| termid | term |
+-----------------------------+
| 1 | Piccadilly line |
| 2 | Circle line |
+-----------------------------+
and
DB2
+-------------------------------------------------------+
| factid | termid | facttype | fact |
+-------------------------------------------------------+
| 1 | 1 | station | Holborn |
| 2 | 1 | station | Green Park |
| 3 | 1 | opened | 1906 |
+-------------------------------------------------------+
I use the following code to query the tables:
SELECT db1.term as `term`,
GROUP_CONCAT(CONCAT(db2.facttype, ': ', db2.fact)) as facts
FROM db1
LEFT JOIN db2
ON db1.`termid` = db2.termid
WHERE db1.`term` = 'Piccadilly line'
GROUP BY db1.term
Which returns the results:
+-------------------------------------------------------------+
| term | facts |
+-------------------------------------------------------------+
| 1 | station: Holborn, station: Green Park, opened: 1906 |
+-------------------------------------------------------------+
How do I need to change the SELECT query so that the following is displayed:
+-------------------------------------------------------------+
| term | facts |
+-------------------------------------------------------------+
| 1 | station: Holborn and Green Park, opened: 1906 |
+-------------------------------------------------------------+
Upvotes: 1
Views: 56
Reputation: 33945
SELECT termid
, GROUP_CONCAT(facts SEPARATOR '; ') facts
FROM
( SELECT f.termid
, CONCAT(facttype,': ',GROUP_CONCAT(fact SEPARATOR ', ')) facts
FROM facts f
GROUP
BY termid
, facttype
) x
GROUP
BY termid;
Upvotes: 1