user3379956
user3379956

Reputation: 51

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('Y-m-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?

Upvotes: 0

Views: 505

Answers (4)

raina77ow
raina77ow

Reputation: 106443

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.

Upvotes: 2

Ria
Ria

Reputation: 526

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

Upvotes: 0

daidoji70
daidoji70

Reputation: 354

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.

Upvotes: 0

Mike Brant
Mike Brant

Reputation: 71414

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

Upvotes: 1

Related Questions