SQLite筛选指定月份数据失败求助:查询无结果排查
Hey there, let's work through why your query isn't pulling up October data. Here are the most likely fixes to try:
1. First, confirm your date column's actual content and type
Before adjusting your query, you need to know exactly how dates are stored in the transaction_date column. Run these two quick checks:
- Check column type:
Look at thePRAGMA table_info(`order`);typevalue fortransaction_date(it'll be TEXT, REAL, or INTEGER). - Sample raw date values:
This will show you the exact format of dates in your table (likeSELECT transaction_date FROM `order` LIMIT 5;2015-10-23,10/23/2015, or a numeric timestamp).
2. Adjust your query based on the date format
Case 1: Dates are stored as SQLite-recognized TEXT formats (YYYY-MM-DD, YYYY-MM-DD HH:MM:SS, etc.)
If your sample dates look like 2015-10-23, your original query should work—but make sure you're escaping the order table name (it's a reserved keyword in SQLite):
SELECT * FROM `order` WHERE strftime('%m', transaction_date) = '10';
Case 2: Dates are in non-standard formats (like MM-DD-YYYY or MM/DD/YYYY)
SQLite's strftime can't parse these automatically, so you need to tell it how to interpret the input format with the date() function:
- For
10-23-2015(MM-DD-YYYY):SELECT * FROM `order` WHERE strftime('%m', date(transaction_date, '%m-%d-%Y')) = '10'; - For
10/23/2015(MM/DD/YYYY):SELECT * FROM `order` WHERE strftime('%m', date(transaction_date, '%m/%d/%Y')) = '10';
Case 3: Dates are stored as numeric timestamps (Unix epoch)
If your transaction_date is a number representing seconds since 1970-01-01, use datetime() to convert it first:
SELECT * FROM `order` WHERE strftime('%m', datetime(transaction_date, 'unixepoch')) = '10';
3. Test with a specific known date
To verify your query works, pick one date from your sample results that you know is in October and filter for it directly:
SELECT * FROM `order` WHERE transaction_date = 'your-october-date-here';
If this returns a row, then adjust your month-filtering query to match the format of that date.
内容的提问来源于stack exchange,提问作者joerna

