我有两张桌子。
'parameters'表(下面)跟踪附加到各个组件的参数:
id (pk) | name | component
'parameterLog'表跟踪一段时间内参数的值:
action_date | parameter (fk) | value | notes
我有以下结果集:
SELECT * FROM parameters WHERE component = 1;
+----+------+-----------+
| id | name | component |
+----+------+-----------+
| 1 | par1 | 1 |
| 2 | par2 | 1 |
+----+------+-----------+
对于上述结果集中的每个条目,我需要'parameterLog'表中相应的和最新的action_date,value和note。
[编辑] 为清楚起见,我将表格包括在下面。
parameters:
+----+------+-----------+
| id | name | component |
+----+------+-----------+
| 1 | par1 | 1 |
| 2 | par2 | 1 |
| 3 | par1 | 2 |
| 4 | par2 | 2 |
| 5 | par1 | 3 |
| 6 | par2 | 3 |
| 7 | par3 | 3 |
| 8 | par1 | 4 |
| 9 | par2 | 4 |
| 10 | par1 | 6 |
| 11 | par2 | 6 |
+----+------+-----------+
parameterLog:
+-------------+-----------+-------+-------+
| action_date | parameter | value | notes |
+-------------+-----------+-------+-------+
| 2010-01-01 | 1 | 0 | NULL |
| 2010-02-03 | 1 | 4 | NULL |
| 2010-01-01 | 2 | 0 | NULL |
| 2010-01-01 | 3 | 1 | NULL |
| 2010-01-01 | 4 | 1 | NULL |
| 2010-01-14 | 5 | 0 | NULL |
| 2010-01-14 | 6 | 1 | NULL |
| 2010-01-14 | 7 | 2 | NULL |
| 2010-01-14 | 8 | 3 | NULL |
| 2010-02-03 | 8 | 1 | NULL |
| 2010-01-14 | 9 | 4 | NULL |
| 2010-02-03 | 9 | 1 | NULL |
| 2010-02-25 | 10 | 9 | NULL |
| 2010-02-25 | 11 | 9 | NULL |
+-------------+-----------+-------+-------+
答案 0 :(得分:0)
SELECT param.*,
plog.action_date,
plog.value,
plog.notes
FROM parameters param,parameter_log plog
WHERE param.id=plog.parameter
and action_date = (select max(action_date)
from parameterLog
where parameter = param.id)
and component = 1;