直接看SQL:
SELECT
`a`.*, `gs`.`market_price`,
`gs`.`shop_price`,
IF( q.q_id IS NULL, 'false', 'true') message_notify
FROM `lb_goods_activity_details` `a`
LEFT JOIN `lb_goods_sku` `gs` ON `gs`.`sku_id` = `a`.`sku_id`
LEFT JOIN `lb_goods_activity` `ga` ON `ga`.`id` = `a`.`activity_id`
LEFT JOIN `lb_queue` `q` ON `q`.`q_goods_id` = a.goods_id
AND q.q_type = 1
AND q.q_user_id = 1
-- 请关注此处的时间戳
AND ( q.q_implement_time >= '09:55' AND q.q_implement_time <= '10:00' )
WHERE (`a`.`status` = '1' AND (`a`.`start_time` >= '10' AND `a`.`end_time` < '16'))
OR ( `a`.`unlimited` = '0' AND `ga`.`type` = '1')
GROUP BY `a`.`goods_id`
LIMIT 0, 10
此条SQL看着没问题,执行起来也没有问题,可就是匹配不到queue任务表中的数据,于是乎我就干脆把时间处理成年月日时分秒格式再次执行,请看下面的SQL:
SELECT
`a`.*, `gs`.`market_price`,
`gs`.`shop_price`,
IF( q.q_id IS NULL, 'false', 'true') message_notify
FROM `lb_goods_activity_details` `a`
LEFT JOIN `lb_goods_sku` `gs` ON `gs`.`sku_id` = `a`.`sku_id`
LEFT JOIN `lb_goods_activity` `ga` ON `ga`.`id` = `a`.`activity_id`
LEFT JOIN `lb_queue` `q` ON `q`.`q_goods_id` = a.goods_id
AND q.q_type = 1
AND q.q_user_id = 1
-- 请关注此处的时间戳
AND (
CONCAT(CURDATE(), ' ', q.q_implement_time, ':00') >= '2020-08-27 9:55:00'
AND CONCAT(CURDATE(), ' ', q.q_implement_time, ':00') <= '2020-08-27 10:00:00'
)
WHERE (`a`.`status` = '1' AND (`a`.`start_time` >= '10' AND `a`.`end_time` < '16'))
OR ( `a`.`unlimited` = '0' AND `ga`.`type` = '1')
GROUP BY `a`.`goods_id`
LIMIT 0, 10
what? 时间戳处理完成了,并且执行也没问题,可还是匹配不到,什么原因?什么原因?于是乎又详细查看,才发现时间格式还不正确'2020-08-27 9:55:00',原因是取到的变量时间,传输的时候没有格式化,于是又把'9:55'这个变量进行格式化,时间不足两位的时候前面补零,咱们再试一下。
SELECT
`a`.*, `gs`.`market_price`,
`gs`.`shop_price`,
IF( q.q_id IS NULL, 'false', 'true') message_notify
FROM `lb_goods_activity_details` `a`
LEFT JOIN `lb_goods_sku` `gs` ON `gs`.`sku_id` = `a`.`sku_id`
LEFT JOIN `lb_goods_activity` `ga` ON `ga`.`id` = `a`.`activity_id`
LEFT JOIN `lb_queue` `q` ON `q`.`q_goods_id` = a.goods_id
AND q.q_type = 1
AND q.q_user_id = 1
-- 请关注此处的时间戳
AND (
CONCAT(CURDATE(), ' ', q.q_implement_time, ':00') >= '2020-08-27 09:55:00'
AND CONCAT(CURDATE(), ' ', q.q_implement_time, ':00') <= '2020-08-27 10:00:00'
)
WHERE (`a`.`status` = '1' AND (`a`.`start_time` >= '10' AND `a`.`end_time` < '16'))
OR ( `a`.`unlimited` = '0' AND `ga`.`type` = '1')
GROUP BY `a`.`goods_id`
LIMIT 0, 10
是的,本次执行没有任何问题,也匹配到了任务表中的值,这个问题让我调了好长时间,以自己的经验一直认为写的没问题,最终还是因为小细节浪费了大把时间;