我正在嘗試加入 codeigniter 3 中的一些表,我使用 group_concat 進行了搜索,但沒有找到任何成功。
這是我的桌子:
產品表
id | flower |
_____________________________
1 | Rose |
2 | Tulip |
場合表:
id | occasion_name |
_____________________________
1 | Valentine |
2 | Mother's Day |
3 | Birthday |
店鋪位置表:
id | city_name |
_____________________________
1 | London |
2 | Belfast |
3 | Bristol |
關系產品_city
id | product_id | product_city
_______________________________________
1 | 1 | 1 |
2 | 1 | 2 |
3 | 2 | 3 |
4 | 2 | 2 |
5 | 2 | 1 |
關系產品_場合
id | product_id | product_occasion
_______________________________________
1 | 1 | 3 |
2 | 1 | 1 |
3 | 2 | 1 |
4 | 2 | 3 |
5 | 2 | 2 |
有可能像這樣計算出我的預期結果嗎?
id | flower | Occasion | City
_____________________________________________________________________________________
1 | Rose | 3-Birthday,1-Valentine | 1-London,2-Belfast
2 | Tulip | 1-Valentine,3-Birthday,2-Mother's Day | 3-Bristol,2-Belfast,1-London
謝謝您的幫助。
uj5u.com熱心網友回復:
采用:
select id,flower,group_concat(DISTINCT Occasion) as Occasion ,group_concat(DISTINCT City) as City
from (
select p.id,p.flower,(concat(o.id,'-',o.occasion_name)) as Occasion ,(concat(sl.id,'-',sl.city_name)) as City
from Product p
inner join product_city pc on p.id=pc.product_id
inner join Store_Location sl on sl.id=pc.product_city
inner join product_occasion po on po.product_id=p.id
inner join Occasion o on o.id=po.product_occasion
group by p.id,p.flower,Occasion,City ) as t1
group by id,flower;
結果:
id flower Occasion City
1 Rose 1-Valentine,3-Birthday 1-London,2-Belfast
2 Tulip 1-Valentine,2-Mother's Day,3-Birthday 1-London,2-Belfast,3-Bristol
演示
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/431252.html
標籤:php mysql 代码点火器 codeigniter-3
上一篇:動態從資料庫中獲取祖先資料
