我有以下查询
SELECT * FROM ".TBL_FOOT_GAMES." ORDER BY id DESC LIMIT 1
我需要在字段WHERE
上添加date_confirmed
子句。
date_confirmed
是DATETIME
类型。
我只需要选择当前时刻7天内的行。
更多代码
SELECT g.home_user, g.away_user, g.home_score, g.away_score, g.id AS gameid, g.date_confirmed,
hu.username AS home_username, au.username AS away_username, ht.team AS home_team, at.team AS away_team
FROM tbl_foot_games g INNER JOIN tbl_users hu ON hu.id = g.home_user INNER JOIN tbl_users au ON au.id = g.away_user
INNER JOIN tbl_foot_teams ht ON ht.id = g.home_team INNER JOIN tbl_foot_teams at ON at.id = g.away_team
WHERE (g.type = '1' OR g.type = '2' OR g.type = '3' OR g.type = '4') AND g.status = '3' AND g.date_confirmed BETWEEN NOW() AND DATE_SUB(NOW(), INTERVAL 50 WEEK)
ORDER BY g.id DESC LIMIT 1
该语句正常工作,直到我为50周的间隔添加WHERE子句。
答案 0 :(得分:1)
未来只假设七天(看起来你将列出即将举行的足球比赛):
SELECT *
FROM `tbl`
WHERE `date_confirmed` BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 1 WEEK)
ORDER BY `id` DESC
LIMIT 1
请在下次首先阅读the documentation;答案都在那里。
答案 1 :(得分:0)
... WHERE date_confirmed BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 7 DAY) ...
答案 2 :(得分:0)
查看NOW()
和DATE_SUB()
函数。
这些应该让你在7天前创建一个日期,然后在你的where子句中你可以检查datetime列是否大于这个。
答案 3 :(得分:0)
您可以使用MySQL的date_sub
函数来查看差异是否为7天或更短。
SELECT * FROM ".TBL_FOOT_GAMES."
WHERE DATE_ADD(DATE_CONFIRMED, INTERVAL '7 00:00:00' DAYS_SECOND) >= TIMESTAMP(CURDATE())
ORDER BY id DESC LIMIT 1
如果您有兴趣只看到与当前日期有7天的差异(忽略时间值),那么您可以使用DATEDIFF
这样的功能:
SELECT * FROM ".TBL_FOOT_GAMES."
WHERE DATEDIFF(CURDATE(), DATE_CONFIRMED) <= 7
ORDER BY id DESC LIMIT 1