MySQL如何完成此选择concat?

时间:2012-04-04 23:49:12

标签: mysql select concat

我有以下表格

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

如何选择concat显示如下

期望的结果:

+---------+-----------+---------------+---------------------------------+
| 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 |
+---------+-----------+---------------+---------------------------------+

我试过

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

1 个答案:

答案 0 :(得分:4)

小提琴中的数据有点不同,所以结果并不完全正确,但这似乎是你想要的:

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