我有 2 张桌子:
money_change:
id_change | id_factor | d_change | date
---------------------------------------------
1 5 0.10 2012-06-07
2 6 0.11 2012-06-07
3 5 0.12 2012-06-09
4 6 0.11 2012-06-10
5 8 1.09 2012-06-03
金钱因素:
id_factor | money_base | money_change
--------------------------------------
5 'Dollar' 'Yen'
6 'Dollar' 'Euro'
7 'Euro' 'Dollar'
8 'Euro' 'Yen'
我正在尝试进行查询,以返回货币对美元的最新变化。我的意思是结果:
id_factor | d_change | date
--------------------------------------
5 0.12 2012-06-09
6 0.11 2012-06-10
我可以知道个别因素的最后日期:
SELECT MAX( date )
FROM money_change
WHERE id_factor = 6
但是您可能知道每个“美元”因素的最后日期?我试过这样的事情:
SELECT id_factor, d_change, date
FROM money_change c
INNER JOIN money_factor f
ON f.id_factor = c.id_factor
LEFT JOIN (
SELECT MAX(date) AS date
FROM money_change
) AS Last
ON c.dRegistro = Last.date
WHERE money_base = "Dollar"
但我回来了:
id_factor | d_change | date
--------------------------------------
5 0.10 2012-06-07
6 0.11 2012-06-07
5 0.12 2012-06-09
6 0.11 2012-06-10
我想我需要在子查询中输入“位置”,但是对于每个因素来说都是动态的?