我遇到了这个问题而我找不到解决方案!
考虑此表payments
:
----------------------------------------------
| id | theday | amount | currency |
----------------------------------------------
| 1 |16/12/2016| 300 | USD |
| 2 |17/12/2016| 200 | USD |
| 3 |16/12/2016| 100 | EUR |
| 4 |17/12/2016| 400 | EUR |
----------------------------------------------
我想要这个输出:
----------------------------
| theday | USD | EUR |
----------------------------
|16/12/2016| 300 | 100 |
|17/12/2016| 200 | 400 |
----------------------------
我尝试了这个查询但没有成功:
SELECT a.`amount` as eur, b.`amount` as dol, a.`theday`
FROM `payments` AS a
JOIN (SELECT `amount` , `theday`, `currency`
FROM `payments`) AS b
WHERE a.`currency` = 'EUR'
AND b.`currency` = 'USD'
最后我不得不将表格结构改为此(id,theday,usd,eur) 我只是想知道是否有一个只使用查询的解决方案。
谢谢。
答案 0 :(得分:0)
我认为你可以通过自我加入来实现:
SELECT t1.theday
,t1.amount
,t2.amount
FROM payments t1
INNER JOIN payments t2 ON t1.theday = t2.theday