使用INTERSECT运算符时出现SQL错误

时间:2014-12-12 13:59:33

标签: php mysql sql database intersect

我正在尝试查找基于类别的搜索和关键字搜索都能找到的结果。但是在phpMyAdmin中,这段代码会带来错误:

SELECT ID, post_title 
FROM wp_5_posts AS p
LEFT JOIN wp_5_term_relationships AS r ON p.ID = r.object_id
WHERE r.term_taxonomy_id
IN (41, 9 )
AND p.post_type = 'attachment'
GROUP BY p.ID
HAVING COUNT(*)>1

INTERSECT

SELECT ID, post_title
FROM wp_5_posts
WHERE post_title
LIKE '%process%'
AND post_type='attachment'

我收到此错误:

“#1064 - 您的SQL语法有错误;请查看与您的MySQL服务器版本对应的手册,以便在'INTERSECTION SELECT ID附近使用正确的语法,post_title FROM wp_5_posts WHERE post_title LIKE'%powd '第11行

有人可以向我解释问题是什么

1 个答案:

答案 0 :(得分:0)

你可以尝试这个(未经测试),但概念应该是这样的

SELECT ID, post_title  from (
  (
    SELECT ID, post_title 
    FROM wp_5_posts AS p
    LEFT JOIN wp_5_term_relationships AS r ON p.ID = r.object_id
    WHERE r.term_taxonomy_id IN (41, 9 ) AND p.post_type = 'attachment'
  )
  UNION ALL
  (
    SELECT ID, post_title
    FROM wp_5_posts
    WHERE post_title LIKE '%process%' AND post_type='attachment'
  )
) AS t1 GROUP BY ID HAVING COUNT(*)>1;