問候, 所看到的許多帖子在這裏和那裏,子查詢比加入慢...我如何使用Join來代替子查詢進行下列查詢?
,但使用任何連接方法我不能找到一種方法,使下面的查詢..所以,我用子查詢。
任何一個可以告訴我如何正確使用加入了以下情況:
表1:
customerID, Name
1, abc
2, xyz
3, qwe
4, zxc
5, asd
and so on
表2:
customerID, Month, OrderNumbers
1, jan, 5
1, feb, 6
2, jan, 8
3, feb, 5
4, mar, 3
and so on..
我需要做報告像這樣:
customer id, name, jan order, feb order, mar order
1, abc, 5, 6, 0
2. xyz, 8, 0, 0
3. qwe, 0, 5, 0
and so on
我使用這個查詢:
select table1.customerID,
table1.Name,
(select table2.Month as jan
where table2.Month = jan),
(select table2.Month as feb
where table2.Month = feb),
(select table2.Month as mar
where table2.Month = mar)
from table1
但這不工作,因爲它應該...
所以,我怎麼能做到這一點?
你需要一個數據透視表。檢查了這一點http://stackoverflow.com/questions/7674786/mysql-pivot-table –