如何在mysql中使用NOT LIKE和JOIN?

时间:2013-03-12 14:23:56

标签: mysql join sql-like

我需要从查询中排除一些记录,其中日期名称在排除值'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后结果为空。没有不喜欢一切正常。

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

如何使其发挥作用?谢谢你的帮助。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

尝试摆脱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