i am facing a strange issue, if i am checking the mysql query with PHP then i didn’t get the exact result according to mysql query.
But the same query if i run then i get the expected result, here is mysql query:
SELECT id, follow_up_datetime, followed_by, follow_up_status, case_time_zone, description, case_id
FROM case_note
WHERE TYPE LIKE '%follow-up-open%'
AND (
follow_up_datetime LIKE '%2013-01-08%'
OR date( follow_up_datetime ) < '2013-01-08'
)
AND follow_up_status <= '1'
GROUP BY case_id
ORDER BY case_id DESC
LIMIT 0 , 30
the result of this query is :

but when i use the same query with php then i get the the wrong ids:
PHP
//current date is "2013-01-08";
$follow_q = "SELECT id, follow_up_datetime, followed_by, follow_up_status, case_time_zone, description, case_id
FROM case_note
WHERE `type` LIKE '%follow-up-open%'
AND (
follow_up_datetime LIKE '%$current_date%'
OR date(follow_up_datetime) < '$current_date'
)
AND follow_up_status <= '1'
GROUP BY case_id
ORDER BY case_id DESC";
$follow_r = mysql_query($follow_q) or die('Error in Query!<br />' . mysql_error());
this query providing me the id’s like 53, 84, 47, 36.
last three id’s are correct but the first is not correct. i want first id is 139. Can anyone please help to find out the exact problem?
UPDATE: here i have update the result of id 53

A few tips concerning your query itself:
Your
follow_up_datetimeshould be a type timestamp or datetimeYou don’t need the
LIKEoperator in your date condition.This can be shortened into
If you always compare to the current date, you can use
CURRENT_DATEDon’t compare a col of type int to a string which will force MySQL to cast all values of the col
follow_up_statusto string.Are you sure you need
LIKEfor the type condition?Would be much faster.
PHP documentation about the MySQL extension:
EDIT:
Just realized you
GROUP BY case_id.If both, id
53and id139have the samecase_idMySQL needs to know which row can be discarded. If you don’t give this information, one of the two rows is discarded more or less randomly.To work around this problem use a aggregate function. in your case
MIN()would deliver the desired result.The whole query cleaned up using PHP heredoc notation: