简体   繁体   中英

MySQL get records by quarters

Have problem to returning results from MySQL by quarters.

Mysql: id , start_datetime , end_datetime and more columns.

I need to return records by start_datetime (for example it's - 2016-03-05 20:12:22 | MySQL column format is DateTime). How can I write MySQL query to display that record in a specific quarter?

I was searching a lot but didn't find anything that would work fo me: I tested this:

SELECT * FROM appointments 
WHERE id_user = 80 
AND DATE_SUB(start_datetime, INTERVAL 1 QUARTER) 
AND hash = 'completed' 

But it's not working correct.

This will give you the results for quarter 1:

SELECT * FROM appointments 
WHERE id_user = 80 
AND QUARTER(start_datetime) = 1 
AND hash = 'completed' 

Change the "1" to 2, 3, or 4 to get the other quarters

Here is the way to get all the data based on quarter inMySql or WordPress.

Note : visited is the datetime column into the table

QUARTER(your date) will give you the current quarter.

Previous Quarter

SELECT * FROM tbl_users WHERE QUARTER(visited) = QUARTER(curdate() - INTERVAL 1 quarter)


Current Quarter

SELECT * FROM tbl_users WHERE QUARTER(visited) = QUARTER(curdate())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM