Following is mysql query in which i am detecteing if the current (All)month&(in current)year exist in the database date column or not. Query is working fine but its running for every row of the table separately and returning all the number of rows present in the table with the specific match for that row. What i wanna do is that the following query should return single row and tells if the given date exist (anywhere in date column) in the table or not . Kindly let me know how can i modify the following query to do this. Thanks
SELECT
IF(DATE_FORMAT(FROM_UNIXTIME(dt_note_created),'%Y%m') = CONCAT((DATE_FORMAT(NOW(),'%Y')),'01'),1,0) as jan,
IF(DATE_FORMAT(FROM_UNIXTIME(dt_note_created),'%Y%m') = CONCAT((DATE_FORMAT(NOW(),'%Y')),'02'),1,0) as feb,
IF(DATE_FORMAT(FROM_UNIXTIME(dt_note_created),'%Y%m') = CONCAT((DATE_FORMAT(NOW(),'%Y')),'03'),1,0) as mar,
IF(DATE_FORMAT(FROM_UNIXTIME(dt_note_created),'%Y%m') = CONCAT((DATE_FORMAT(NOW(),'%Y')),'04'),1,0) as apr,
IF(DATE_FORMAT(FROM_UNIXTIME(dt_note_created),'%Y%m') = CONCAT((DATE_FORMAT(NOW(),'%Y')),'05'),1,0) as may,
IF(DATE_FORMAT(FROM_UNIXTIME(dt_note_created),'%Y%m') = CONCAT((DATE_FORMAT(NOW(),'%Y')),'06'),1,0) as jun,
IF(DATE_FORMAT(FROM_UNIXTIME(dt_note_created),'%Y%m') = CONCAT((DATE_FORMAT(NOW(),'%Y')),'07'),1,0) as jul,
IF(DATE_FORMAT(FROM_UNIXTIME(dt_note_created),'%Y%m') = CONCAT((DATE_FORMAT(NOW(),'%Y')),'08'),1,0) as aug,
IF(DATE_FORMAT(FROM_UNIXTIME(dt_note_created),'%Y%m') = CONCAT((DATE_FORMAT(NOW(),'%Y')),'09'),1,0) as sep,
IF(DATE_FORMAT(FROM_UNIXTIME(dt_note_created),'%Y%m') = CONCAT((DATE_FORMAT(NOW(),'%Y')),'10'),1,0) as oct,
IF(DATE_FORMAT(FROM_UNIXTIME(dt_note_created),'%Y%m') = CONCAT((DATE_FORMAT(NOW(),'%Y')),'11'),1,0) as nov,
IF(DATE_FORMAT(FROM_UNIXTIME(dt_note_created),'%Y%m') = CONCAT((DATE_FORMAT(NOW(),'%Y')),'12'),1,0) as decb
FROM Notes
Output:
jan | feb | Mar ....and so on... | nov | dec 0 1 1 0 1 0 and 1 are flags which are telling if the monthandyear exist in the whole column of date or not
I’m not sure if I understood correctly, but if u only want to check if there is at least one date in your column matching current month and year, try
this will return 0 if there is no such date and otherwise the number of dates in total.
Try
to get a list of the differnet month included in your date column.
it returns something like (for dates in Jan, Feb, Aug, Nov):
And as long as this number is less than 12 there is not at least one date in each month.