SQL查询返回昨日及今日数据,需修改为仅获取当日数据
Fixing the SQL Query to Return Only Current Date Data
Got it, let's sort out your query to meet your requirement. The original statement is pulling in both yesterday's and today's data because the BETWEEN clause spans a 24-hour window starting yesterday. Here's how to adjust it:
Adjusted Query (for DATE type columns)
If your date column is a pure DATE type (no time component), you can simplify the condition to check for an exact match with today's date:
SELECT * FROM branches WHERE NOT EXISTS ( SELECT * FROM mytable WHERE mytable.branch_code = branches.Branch_Code AND mytable.date = CURDATE() )
Adjusted Query (for DATETIME/TIMESTAMP type columns)
If your date column includes a time component (like 2024-05-20 14:30:00), you'll want to cast it to a date first to ignore the time part:
SELECT * FROM branches WHERE NOT EXISTS ( SELECT * FROM mytable WHERE mytable.branch_code = branches.Branch_Code AND DATE(mytable.date) = CURDATE() )
What Changed?
- Removed the
DATE_SUBandBETWEENlogic that was including yesterday's data. - Replaced it with an exact match to
CURDATE()to only target today's records. - For datetime/timestamp columns, added
DATE()aroundmytable.dateto ignore time components and match on the date alone.
This will return all branches that have no corresponding records in mytable for the current date—exactly what you need!
内容的提问来源于stack exchange,提问作者Priyankannie
相关产品推荐
相关产品推荐

