문제

How would I write a SQLite query to select all records from a specific month? My dates are stored as Unix timestamps.

PHP code is allowed in your solution if it's required. SQLite2-compatible queries only, please. :)

도움이 되었습니까?

해결책

If you want an efficient query then you should use BETWEEN:

SELECT *
FROM Table1
WHERE date BETWEEN $start AND $end

Where start and end are the unix timestamps for the start of the month and the end of the month. These can be calculated in PHP using mktime and sent as parameters.

다른 팁

Use:

SELECT *
  FROM TABLE 
 WHERE DATETIME(your_unix_timestamp_col, 'unixepoch') BETWEEN YYYY-MM-DD 
                                                          AND YYYY-MM-DD

Replace the YYYY-MM-DD with the dates you desire. See the reference page for other supported formats.

Reference:

Without calculating last day in month

SELECT * 
  FROM table 
 WHERE strftime('%Y-%m',date(unix_timestamp,'unixepoch','localtime')) = '2010-03'
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top