如何编写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
ORensures we include every record that falls within your desired date window.
When you run this, you'll get exactly the result set you specified:
| name | salary | day | month |
|---|---|---|---|
| marie | 400.00 | 4 | january |
| jimmy | 300.00 | 7 | january |
| Fredd | 700.00 | 3 | february |
| rosieli | 500.00 | 5 | february |
内容的提问来源于stack exchange,提问作者Rodney Nart
相关产品推荐
相关产品推荐

