简体   繁体   中英

How to add where clause and how to get the month difference of two dates

How to count columns when parapameter?

I need to count all clients which when you subtract todays date/current(ex; 2013-10-28) to birth_date column(ex; 2001-15-13) returns "<= 6 months"

I have a this mysql query:

$query = 'SELECT  b.record_number,
CONCAT(b.fname," ",b.lname) AS fullname,
province.area_name AS province,
district.area_name AS district,
llg.area_name AS llg,
office.area_name AS office, 
c.clinic_name,
a.feeding_type, 
COUNT(NOW, b.date_birth), - this is my code
b.date_birth,               
FROM tbl_records AS a
JOIN tbl_client AS b ON a.client_id = b.ID
JOIN tbl_clinic AS c ON a.clinic_id = c.ID
JOIN tbl_area AS llg ON c.llg_id = llg.ID
JOIN tbl_area AS district ON llg.parent_ids = district.ID
JOIN tbl_area AS province ON district.parent_ids = province.ID 
JOIN tbl_area AS office ON office.ID = a.office_id
WHERE a.date >= :start_date AND a.date <= :end_date 
AND a.feeding_type <> "NULL" AND a.office_id = :office_id
GROUP BY a.client_id';

To get the difference between dates in MYSQL, you can use DATEDIFF

http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_datediff

From your question, I think that you are trying to return some intervals and return a string based on that interval. You will need to set up a series of IF statements.

https://dev.mysql.com/doc/refman/5.0/en/if.html

UPDATE:

You can get the difference in months with PERIOD-DIFF but you will need to change the format of the dates. Which you can do using GET_FORMAT

http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_get-format

http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_period-diff

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