Re: User-friendly approaches to displaying subtable data
Posted by: Rick James
Date: June 23, 2013 12:47AM

SELECT rec_num, GROUP_CONCAT(location)
FROM tbl
GROUP BY rec_num;

Example:
mysql> SELECT state, GROUP_CONCAT(city) AS 'big cities'
    -> FROM us
    -> WHERE population > 600000
    -> GROUP BY state;
+-------+----------------------------------------------+
| state | big cities                                   |
+-------+----------------------------------------------+
| AZ    | Phoenix                                      |
| CA    | Los Angeles,San Diego,San Francisco,San Jose |
| FL    | Jacksonville                                 |
| IL    | Chicago                                      |
| IN    | Indianapolis                                 |
| MD    | Baltimore                                    |
| MI    | Detroit                                      |
| NY    | New York                                     |
| OH    | Columbus                                     |
| PA    | Philadelphia                                 |
| TN    | Memphis                                      |
| TX    | Austin,Dallas,Fort Worth,Houston,San Antonio |
+-------+----------------------------------------------+

(JOIN and LEFT JOIN are not critical to this answer, since the one-to-many relationship is embodied in a single table.)

Options: ReplyQuote




Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.