我已经阅读了很多与这个问题非常类似的问题和答案,主要问题是他们使用LIMIT
作为解决方案来返回最新的记录,吨与这个查询工作,因为我需要几个结果回来。MYSQL - 返回GROUP BY前的最近记录
逻辑如下为:
- 每个记录具有
effective_date
,当记录值生效。 - 只有拉回来的记录谁是
effective_date
的是今天或已通过 - 我拉回来的记录,给我基础上,最近的记录“
date_entered
”该记录
到目前为止通过此查询,我能够完成除了最后一点逻辑之外的所有内容。我不能在GROUP BY
之后使用ORDER BY
,因为结果已经被分组,因此我无法使用MAX()
日期。我需要做WHERE
声明中的逻辑。
SELECT
aos_products.`name`,
cac_customize_agent_comp_cstm.commission_percentage_c,
cac_customize_agent_comp.date_entered
FROM
wn_writing_number
LEFT OUTER JOIN wn_writing_number_cac_customize_agent_comp_1_c ON wn_writing_number.id = wn_writing_number_cac_customize_agent_comp_1_c.wn_writing946b_number_ida
LEFT OUTER JOIN cac_customize_agent_comp_cstm ON wn_writing_number_cac_customize_agent_comp_1_c.wn_writing3148nt_comp_idb = cac_customize_agent_comp_cstm.id_c
LEFT OUTER JOIN cac_customize_agent_comp ON cac_customize_agent_comp_cstm.id_c = cac_customize_agent_comp.id
LEFT OUTER JOIN aos_products_cac_customize_agent_comp_1_c ON cac_customize_agent_comp_cstm.id_c = aos_products_cac_customize_agent_comp_1_c.aos_produca2b8nt_comp_idb
LEFT OUTER JOIN aos_products ON aos_products_cac_customize_agent_comp_1_c.aos_products_cac_customize_agent_comp_1aos_products_ida = aos_products.id
WHERE
wn_writing_number.id = 'b556d816-f2e6-b78f-c776-576c0542585c'
AND wn_writing_number_cac_customize_agent_comp_1_c.deleted = '0'
AND cac_customize_agent_comp_cstm.effective_date_c <= CURDATE()
GROUP BY aos_products.`name`
没有GROUP BY,则返回:
+---------+-------------------------+----------------------+
| name | commission_percentage_c | date_entered |
+---------+-------------------------+----------------------+
| Val 2 | 14.75 | 2016-07-07 15:04:02 |
+---------+-------------------------+----------------------+
| Val 1 | 16 | 2016-07-07 00:31:08 |
+---------+-------------------------+----------------------+
:
(varchar) (varchar) (datetime)
+---------+-------------------------+----------------------+
| name | commission_percentage_c | date_entered |
+---------+-------------------------+----------------------+
| Val 1 | 15.25 | 2016-07-06 23:57:28 |
+---------+-------------------------+----------------------+
| Val 1 | 15.75 | 2016-07-07 00:03:03 |
+---------+-------------------------+----------------------+
| Val 1 | 16 | 2016-07-07 00:31:08 |
+---------+-------------------------+----------------------+
| Val 2 | 14.75 | 2016-07-07 15:04:02 |
+---------+-------------------------+----------------------+
与返回
+---------+-------------------------+----------------------+
| name | commission_percentage_c | date_entered |
+---------+-------------------------+----------------------+
| Val 2 | 14.75 | 2016-07-07 15:04:02 |
+---------+-------------------------+----------------------+
| Val 1 | 15.25 | 2016-07-06 23:57:28 |
+---------+-------------------------+----------------------+
我试图找回是集团
因为我需要最新的VAL 1记录d回来,像使用
WHERE the datediff BETWEEN NOW() and date_entered = the smallest amount of time compared with other records
使用'max()'为date_entered – CSK
[检索每个组中的最后一条记录]的可能重复(http://stackoverflow.com/questions/1313120/retrieving-the-last-record-in-each-group ) – imtheman