简体   繁体   中英

MySQL How do I complete this select concat?

I have the following tables

http://sqlfiddle.com/#!2/647e9

How do I select concat to display as followed

Desired Results:

+---------+-----------+---------------+---------------------------------+
| Origin  | Stock     | Farm Title    |            Farm Values          |
+---------+-----------+---------------+---------------------------------+
| US      | P1        | Perdue        |                             333 |
| US      | P3        | Holstein      |                             825 | 
| CA      | Q4        | FarmOne       |         455,536,617,698,779,860 |
| CA      | Q4        | Beef Farm     |         540,550,560,570,580,590 |
| CA      | Q4        | CattleOne     |   1080,1100,1120,1140,1160,1180 |
| MX      | B3        | Cow Mill      |                              11 |
| MX      | B3        | Dotterers     |                              98 | 
| MX      | B3        | AgriZone      |                             202 |
+---------+-----------+---------------+---------------------------------+

I tried

Select
ORIGIN_NAME
STOCK_TITLE
FARM_TITLE
concat(FARM_VALUES, ",")

From Farm f
JOIN STOCK s on S.S_ID=f.S_ID
JOIN ORIGIN o on o.ORI_ID=s.ORI_ID

The data is a bit different in the fiddle, so the results are not exactly right, but this seems to be what you're going for:

select origin_name, stock_title, farm_title, group_concat(FARM_COMPONENETS)  from ORIGIN o
inner join STOCK s on o.ORIGIN_ID = s.ORIGIN_ID
inner join FARM f on s.stock_id = f.stock_id 
inner join gate g on f.farm_id = g.farm_id
group by origin_name, stock_title, farm_title

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM