我已经阅读了很多关于非常类似问题的问题和答案,主要问题是他们使用LIMIT
作为返回最近记录的解决方案查询,因为我需要几个结果。
逻辑如下:
effective_date
。effective_date
已成为或已经过去的记录date_entered
'给我最新记录。该记录到目前为止,我已经能够使用此查询完成除最后一点逻辑之外的所有操作。我无法在ORDER BY
之后使用GROUP 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,它返回:
(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 |
+---------+-------------------------+----------------------+
WITH GROUP BY返回
+---------+-------------------------+----------------------+
| 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 |
+---------+-------------------------+----------------------+
我想回来的是:
+---------+-------------------------+----------------------+
| 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 |
+---------+-------------------------+----------------------+
因为我需要最新的VAL 1记录,比如
WHERE the datediff BETWEEN NOW() and date_entered = the smallest amount of time compared with other records
答案 0 :(得分:3)
试试这个
SELECT *
FROM
(SELECT
aos_products.`name` as name,
cac_customize_agent_comp_cstm.commission_percentage_c as commission_percentage_c,
cac_customize_agent_comp.date_entered as 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()
ORDER BY cac_customize_agent_comp.date_entered DESC) t
GROUP BY t.name
这样做的想法是使用date_entered
按group by
对记录进行排序。从this post开始,groupby
将使用与正常SELECT
相同顺序的第一个选择。
答案 1 :(得分:0)
使用max()获取最新日期时间
SELECT
aos_products.`name`,
cac_customize_agent_comp_cstm.commission_percentage_c,
max(cac_customize_agent_comp.date_entered) as 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`