我正在编写一个查询,它将在上周查看并选择一个值,并给我计算它被使用...非常简单的概念,除了当我运行查询时,我得到一个结果,系统不是'上周使用了!
SELECT re.topic, COUNT(*) AS TimesUsed
FROM ost_ticket AS us
JOIN ost_help_topic AS re ON re.topic_id = us.topic_id
WHERE re.created >= CURDATE() - INTERVAL (WEEKDAY(CURDATE())+7) DAY
AND re.created < CURDATE() - INTERVAL (WEEKDAY(CURDATE())) DAY
GROUP BY us.topic_id ORDER BY TimesUsed DESC LIMIT 1
我可以确认上周没有门票,因为当我运行查询以查看上周是否创建了任何答案为空时。
SELECT number
FROM ost_ticket
WHERE created >= CURDATE() - INTERVAL (WEEKDAY(CURDATE())+7) DAY
AND created < CURDATE() - INTERVAL (WEEKDAY(CURDATE())) DAY;
所以我的问题基本上是我在日期选择上做错了以获得最后几周的日期?
答案 0 :(得分:0)
您正在混合dos2unix
和日期值。如果您想检查系统是否在上周使用,只需从intervals
中减去7天的时间间隔:
curdate()
答案 1 :(得分:0)
我能够从select语句中选择以获取自上一个星期一以来的日期所需的列。
SELECT
t1.topic
FROM
(SELECT
re.topic AS topic, COUNT(*) AS TimesUsed
FROM
ost_ticket AS us
JOIN ost_help_topic AS re ON re.topic_id = us.topic_id
WHERE
re.created BETWEEN DATE(DATE_SUB(DATE_SUB(NOW(), INTERVAL ((7 + WEEKDAY(DATE_SUB(NOW(), INTERVAL 1 WEEK)) - 4) % 7) DAY), INTERVAL 4 DAY)) AND DATE_FORMAT(DATE_SUB(NOW(), INTERVAL ((7 + WEEKDAY(DATE_SUB(NOW(), INTERVAL 1 WEEK)) - 4) % 7) DAY), '%Y-%m-%d')
GROUP BY us.topic_id
ORDER BY TimesUsed DESC
LIMIT 1) t1;