简体   繁体   English

MySQL大于日期返回的日期小于给定的日期

[英]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')); 我在使用NOW(),CURDATE()和php date('Ym-d',strtotime('now'))时,MySQL给我的结果的日期小于今天的日期时遇到问题; 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... 您可能需要将查询中与OR相关的部分更改为此...

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). 请参阅, OR优先级低于AND (与+运算符的优先级低于*一相同)。 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. 您的WHERE子句中的运算符优先级可能有问题。

Because AND takes precedence over OR , you are in effect doing this: 因为AND优先于OR ,所以您实际上是在这样做:

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. 因此,将选择满足OR语句一半的任何记录。

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: 有关MySQL中运算符优先级的更多信息,请查看以下链接:

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

AND's take precedence over OR's. AND的优先级高于OR的优先级。 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. 因此,您的错误日期可能来自上述查询中where子句的第一部分。

You are mixing the and and or statements the order might change in the way thing are selected 您正在混合and和or语句,顺序可能会以选择方式改变

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 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM