You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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_SUB and BETWEEN logic 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() around mytable.date to 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:16:58