【问题标题】:How to use NOT LIKE and JOIN together in mysql?如何在 mysql 中同时使用 NOT LIKE 和 JOIN?
【发布时间】:2013-03-12 14:23:55
【问题描述】:

我需要从查询中排除一些记录,其中日期名称设置在排除值“excl_days”中。

简单的功能正在运行:

SELECT post_id, start, end, excl_days FROM `mytable_events` 
WHERE `excl_days` NOT LIKE '%".$today_name."%' 
ORDER BY DATE(start) DESC

但是使用 JOIN 和 GROUP 在添加 NOT LIKE 后我的结果为空。没有 NOT LIKE 一切正常。

SELECT o1.post_id, o1.start, o1.end, o1.excl_days 
FROM `mytable_events` o1 
JOIN `mytable_posts` o2 ON o1.post_id = o2.ID 
WHERE `o1.excl_days` NOT LIKE '%".$today_name."%'
GROUP BY o1.post_id ORDER BY DATE(o1.start) DESC

完整的查询是:

SELECT o1.post_id, o1.start, o1.end, o1.excl_days 
FROM `mytable_events` o1 
JOIN `mytable_posts` o2 ON o1.post_id = o2.ID 
WHERE DATE(o1.start) <= '".$today."' AND DATE(o1.end) >= '".$today."'
AND o2.post_type = 'events' AND o2.post_status = 'publish' 
AND `o1.excl_days` NOT LIKE '%".$today_name."%'
GROUP BY o1.post_id ORDER BY DATE(o1.start) DESC

如何使它工作?谢谢你的帮助。

【问题讨论】:

    标签: mysql join sql-like


    【解决方案1】:

    试试这个:

      SELECT o1.post_id, o1.start, o1.end, o1.excl_days 
      FROM `mytable_events` o1 
      JOIN `mytable_posts` o2 ON (o1.post_id = o2.ID 
      AND o1.excl_days NOT LIKE '%".$today_name."%')
      GROUP BY o1.post_id ORDER BY DATE(o1.start) DESC
    

    【讨论】:

      【解决方案2】:

      尝试去掉 where 子句中的反引号。它们是不必要的,而且格式不正确。

      SELECT o1.post_id, o1.start, o1.end, o1.excl_days 
      FROM `honchar_events` o1 
      JOIN `honchar_posts` o2 ON o1.post_id = o2.ID 
      WHERE o1.excl_days NOT LIKE '%".$today_name."%'
      GROUP BY o1.post_id ORDER BY DATE(o1.start) DESC
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-09
        • 2010-11-26
        • 1970-01-01
        相关资源
        最近更新 更多