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

如何编写SQL查询筛选db_table中1月4日至2月5日的记录?

Correct SQL Query to Filter Date Range (Jan 4 to Feb 5)

Got it, let's get that query sorted for you. Your original attempt had the right idea but the syntax and logic for handling separate day and month columns was off—you can't use BETWEEN directly across two different columns like that. Here's the working query that will return exactly the records you want:

SELECT * 
FROM db_table
WHERE 
    (month = 'january' AND day >= 4)
    OR 
    (month = 'february' AND day <= 5);

Why this works:

  • The first condition (month = 'january' AND day >= 4) grabs all January records from the 4th onwards (which includes Marie and Jimmy's entries).
  • The second condition (month = 'february' AND day <= 5) captures all February records up to and including the 5th (covering Fredd and Rosieli's rows).
  • Combining them with OR ensures we include every record that falls within your desired date window.

When you run this, you'll get exactly the result set you specified:

namesalarydaymonth
marie400.004january
jimmy300.007january
Fredd700.003february
rosieli500.005february

内容的提问来源于stack exchange,提问作者Rodney Nart

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 09:21:37