MySQL內連接group_concat [重復]

[英]MySQL inner join group_concat [duplicate]


This question already has an answer here:

這個問題在這里已有答案:

I have 4 MySQL Tables shown here:SQL Fiddle

我在這里顯示了4個MySQL表:SQL Fiddle

I need to select station name, brand, fuel_type and fual_price for each station in given set and fuel typ in given set, e.q all Shell stations with fuel Diesel and Natural.

我需要為給定組中的每個站點選擇站名,品牌,fuel_type和fual_price,並在給定的組中選擇燃料類型,e.q所有Shell站點都有燃料柴油和自然燃料。

I would like to have all data related to one station in one row, which means I have e.g. 5 fuel types/prices for 1 station.

我希望將所有與一個站相關的數據放在一行中,這意味着我有例如1站的5種燃料類型/價格。

Example output -

示例輸出 -

id | name     | brand | fuel-data
---------------------------------------------------------
1  | Brzotice | Shell | Natural95-37.9, Diesel Power-45.8

Could you please help me?

你可以幫我嗎?

1 个解决方案

#1


3  

I think this will give you want you want:

我想這會讓你想要你想要的:

select s.id, 
  s.name,
  b.brand,
  group_concat(concat(f.type, '-', fs.price)) Fuel_Data
from sam_fuel f
left join sam_station_fuel fs
  on f.id = fs.id_fuel
left join sam_station s
  on fs.id_station = s.id
left join sam_station_brand b
  on s.brand = b.id
group by s.id, s.name, b.brand

See SQL Fiddle with Demo

請參閱SQL Fiddle with Demo

Basically this query joins all of your tables. Then to get the fuel_data field it applies both the CONCAT() and the GROUP_CONCAT() functions

基本上這個查詢加入了你的所有表。然后,為了獲得fuel_data字段,它同時應用CONCAT()和GROUP_CONCAT()函數


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/10/03/3ea9ba5d06d2caa1d6e326f809f1543a.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com