Forum Moderators: open

Message Too Old, No Replies

MySQL query help

         

artie2004

10:09 pm on Oct 13, 2006 (gmt 0)

10+ Year Member



Hi. I have the following table

transactions(id,account,date,description,amount,type).

How do i query all rows for the current year? Thanks.

coopster

10:14 pm on Oct 13, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Is that
date
column of type
DATE
? By the way, although you can get away with it you really shouldn't use reserved words for column names.

[dev.mysql.com...]

artie2004

10:19 pm on Oct 13, 2006 (gmt 0)

10+ Year Member



hey coopster. it's type datetime.

coopster

11:12 pm on Oct 13, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If your database supports the YEAR function you could simply ...
SELECT * FROM transactions WHERE YEAR(`date`) = YEAR(CURRENT_DATE);

artie2004

11:23 pm on Oct 13, 2006 (gmt 0)

10+ Year Member



your awesome coopster. thanks.

artie2004

12:16 am on Oct 14, 2006 (gmt 0)

10+ Year Member



hey coopster. How do i query all the rows for the current year up to todays date? Thanks.

coopster

12:29 am on Oct 14, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Up to today's date? The query above will do that. It grabs every entry in your database table that has the same year as the year in the current date, which would be every day from January 1 through today.

artie2004

12:38 am on Oct 14, 2006 (gmt 0)

10+ Year Member



Sorry about that. What i meant is how do i query all the rows for the current year up to lets say the date five days ago? Thanks.

coopster

1:48 am on Oct 14, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You just need to add to the query. Check first for the same year as shown, and then add to the clause by checking for dates where the column is less than (or equal to)
CURRENT_TIMESTAMP - INTERVAL 5 DAY
.
SELECT 
*
FROM transactions
WHERE
YEAR(`date`) = YEAR(CURRENT_DATE) AND
`date` <= CURRENT_TIMESTAMP - INTERVAL 5 DAY
;

artie2004

11:35 am on Oct 14, 2006 (gmt 0)

10+ Year Member



thank you.