简体   繁体   中英

MySQL Greater Than Date Returning A Date Less Than Date Given

I am having a problem with MySQL giving me a result with a date less than todays date when am using NOW(), CURDATE() and php date('Ym-d', strtotime('now')); below is my query and my result

SELECT j.*
       ,j.id AS job_id
       ,c.company_name
       ,images.*
    FROM jobs j
       ,companies c
       ,images images
    WHERE j.user_id = c.user_id
        AND images.user_id = j.user_id
        AND j.description LIKE '%php%'
        OR j.title LIKE '%php%'
        AND j.start_date <= '2014-03-19'
        AND j.end_date > '2014-03-19'
        AND j.published = 1
    GROUP BY j.id ASC
    ORDER BY j.featured DESC

RESULT

start_date: 2013-12-08     end_date:  2014-01-08

is there a reason it would be giving me back this result?

You probably need to change the OR -related part of your query into this...

AND (j.description LIKE '%php%'  OR  j.title LIKE '%php%')

See, OR precedence is lower than AND (the same as + operator's precedence is lower than * one). So it basically splits the whole set of conditions in two parts, making the whole condition to pass the check if any of these parts pass the check.

You likely have a problem with operator precedence in your WHERE clause.

Because AND takes precedence over OR , you are in effect doing this:

WHERE
(j.user_id = c.user_id
AND images.user_id = j.user_id
AND j.description LIKE '%php%')
OR
(j.title LIKE '%php%'
AND j.start_date <= '2014-03-19' 
AND j.end_date > '2014-03-19'
AND j.published =1)

So any records that fulfill either half of the OR statement will be selected.

I am guessing you want to do this:

WHERE
j.user_id = c.user_id
AND images.user_id = j.user_id
AND (j.description LIKE '%php%' OR j.title LIKE '%php%')
AND j.start_date <= '2014-03-19' 
AND j.end_date > '2014-03-19'
AND j.published =1

My suggestion is to get in the habit of using parenthesis in ALL cases where you are mixing operators like this so it is clear to you, and anyone else who might read the code, exactly what the intent is.

For more on operator precedence in MySQL, check out the following link:

https://dev.mysql.com/doc/refman/5.6/en/operator-precedence.html

AND's take precedence over OR's. What this means is that instead of doing

SELECT j . * , j.id AS job_id, c.company_name, images . *
FROM jobs j, companies c, images images
WHERE j.user_id = c.user_id
AND images.user_id = j.user_id
AND ( j.description LIKE '%php%'OR j.title LIKE '%php%' )
AND j.start_date <= '2014-03-19' 
AND j.end_date > '2014-03-19'
AND j.published =1
GROUP BY j.id ASC
ORDER BY j.featured DESC

like it seems that condition makes more sense doing. You're instead performing

SELECT j . * , j.id AS job_id, c.company_name, images . *
FROM jobs j, companies c, images images
WHERE ( 
   j.user_id = c.user_id
   AND images.user_id = j.user_id
   AND j.description LIKE '%php%' 
)
OR 
(
   j.title LIKE '%php%' 
   AND j.start_date <= '2014-03-19' 
   AND j.end_date > '2014-03-19'
   AND j.published = 1 
)
GROUP BY j.id ASC
ORDER BY j.featured DESC

So your errant dates are probably coming from the first part of that where clause in the query above.

You are mixing the and and or statements the order might change in the way thing are selected

SELECT j . * , j.id AS job_id, c.company_name, images . *
FROM jobs j, companies c, images images
WHERE j.user_id = c.user_id
AND images.user_id = j.user_id
AND j.description LIKE '%php%'
OR (j.title LIKE '%php%'
AND j.start_date <= '2014-03-19' 
AND j.end_date > '2014-03-19'
AND j.published =1)
GROUP BY j.id ASC
ORDER BY j.featured DESC

Try putting the brackets in the right place. Hope that works. Good luck

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