MySQL从3个选项中获得相同的值到1

时间:2016-07-11 16:08:24

标签: mysql

我有3个选择:

SELECT wp_e398be30f4_posts.*
FROM wp_e398be30f4_posts
INNER JOIN wp_e398be30f4_postmeta ON (wp_e398be30f4_posts.ID = wp_e398be30f4_postmeta.post_id)
INNER JOIN wp_e398be30f4_term_relationships ON (wp_e398be30f4_posts.ID = wp_e398be30f4_term_relationships.object_id)
WHERE 1=1
  AND wp_e398be30f4_posts.post_type = 'offer'
  AND ((wp_e398be30f4_posts.post_status = 'active'))
  AND ((wp_e398be30f4_term_relationships.term_taxonomy_id IN (33)
        AND (0 = 1
             OR 0 = 1))
       OR (((wp_e398be30f4_postmeta.meta_key = 'title'
             AND CAST(wp_e398be30f4_postmeta.meta_value AS CHAR) LIKE '%var1%')
            OR (wp_e398be30f4_postmeta.meta_key = 'content'
                AND CAST(wp_e398be30f4_postmeta.meta_value AS CHAR) LIKE '%var1%'))))
GROUP BY wp_e398be30f4_posts.ID
ORDER BY wp_e398be30f4_posts.post_date DESC

第二

SELECT wp_e398be30f4_posts.*
FROM wp_e398be30f4_posts
INNER JOIN wp_e398be30f4_postmeta ON (wp_e398be30f4_posts.ID = wp_e398be30f4_postmeta.post_id)
INNER JOIN wp_e398be30f4_term_relationships ON (wp_e398be30f4_posts.ID = wp_e398be30f4_term_relationships.object_id)
WHERE 1=1
  AND wp_e398be30f4_posts.post_type = 'offer'
  AND ((wp_e398be30f4_posts.post_status = 'active'))
  AND ((wp_e398be30f4_term_relationships.term_taxonomy_id IN (33)
        AND (0 = 1
             OR 0 = 1))
       OR (((wp_e398be30f4_postmeta.meta_key = 'title'
             AND CAST(wp_e398be30f4_postmeta.meta_value AS CHAR) LIKE '%var2%')
            OR (wp_e398be30f4_postmeta.meta_key = 'content'
                AND CAST(wp_e398be30f4_postmeta.meta_value AS CHAR) LIKE '%var2%'))))
GROUP BY wp_e398be30f4_posts.ID
ORDER BY wp_e398be30f4_posts.post_date DESC

THIRD:

SELECT wp_e398be30f4_posts.*
FROM wp_e398be30f4_posts
INNER JOIN wp_e398be30f4_postmeta ON (wp_e398be30f4_posts.ID = wp_e398be30f4_postmeta.post_id)
INNER JOIN wp_e398be30f4_term_relationships ON (wp_e398be30f4_posts.ID = wp_e398be30f4_term_relationships.object_id)
WHERE 1=1
  AND wp_e398be30f4_posts.post_type = 'offer'
  AND ((wp_e398be30f4_posts.post_status = 'active'))
  AND ((wp_e398be30f4_term_relationships.term_taxonomy_id IN (33)
        AND (0 = 1
             OR 0 = 1))
       OR (((wp_e398be30f4_postmeta.meta_key = 'title'
             AND CAST(wp_e398be30f4_postmeta.meta_value AS CHAR) LIKE '%var3%')
            OR (wp_e398be30f4_postmeta.meta_key = 'content'
                AND CAST(wp_e398be30f4_postmeta.meta_value AS CHAR) LIKE '%var3%'))))
GROUP BY wp_e398be30f4_posts.ID
ORDER BY wp_e398be30f4_posts.post_date DESC

正如你可以看到他们是一样的,但我改变了价值观。现在我需要为所有3个查询仅过滤相同的值。

所以如果第一个选择给我ID 1,2,3,4,5,6 secound将给我2,3,6和第三 - 2,6我需要得到只有2和6。

我希望你知道我想说的话:(

我试过通过使用INTERSECT,INNER JOIN,UNION来展示它们,我不记得还有什么,但总是有这样的错误:

enter image description here

有人可以帮助我吗?

感谢您的所有评论:)

2 个答案:

答案 0 :(得分:1)

我没有看到聚合函数,所以你不应该在subselect中使用group by但是distinct和order by 并且您可以在表格之间使用内部联接

select * from ( 

    SELECT distinct wp_e398be30f4_posts.*
    FROM wp_e398be30f4_posts
    INNER JOIN wp_e398be30f4_postmeta ON (wp_e398be30f4_posts.ID = wp_e398be30f4_postmeta.post_id)
    INNER JOIN wp_e398be30f4_term_relationships ON (wp_e398be30f4_posts.ID = wp_e398be30f4_term_relationships.object_id)
    WHERE 1=1
      AND wp_e398be30f4_posts.post_type = 'offer'
      AND ((wp_e398be30f4_posts.post_status = 'active'))
      AND ((wp_e398be30f4_term_relationships.term_taxonomy_id IN (33)
            AND (0 = 1
                 OR 0 = 1))
           OR (((wp_e398be30f4_postmeta.meta_key = 'title'
                 AND CAST(wp_e398be30f4_postmeta.meta_value AS CHAR) LIKE '%var1%')
                OR (wp_e398be30f4_postmeta.meta_key = 'content'
                    AND CAST(wp_e398be30f4_postmeta.meta_value AS CHAR) LIKE '%var1%'))))
 ) t1 
Inner join ( 
    SELECT distinct wp_e398be30f4_posts.*
    FROM wp_e398be30f4_posts
    INNER JOIN wp_e398be30f4_postmeta ON (wp_e398be30f4_posts.ID = wp_e398be30f4_postmeta.post_id)
    INNER JOIN wp_e398be30f4_term_relationships ON (wp_e398be30f4_posts.ID = wp_e398be30f4_term_relationships.object_id)
    WHERE 1=1
      AND wp_e398be30f4_posts.post_type = 'offer'
      AND ((wp_e398be30f4_posts.post_status = 'active'))
      AND ((wp_e398be30f4_term_relationships.term_taxonomy_id IN (33)
            AND (0 = 1
                 OR 0 = 1))
           OR (((wp_e398be30f4_postmeta.meta_key = 'title'
                 AND CAST(wp_e398be30f4_postmeta.meta_value AS CHAR) LIKE '%var2%')
                OR (wp_e398be30f4_postmeta.meta_key = 'content'
                    AND CAST(wp_e398be30f4_postmeta.meta_value AS CHAR) LIKE '%var2%'))))
    )    t2 on t1.id = t2.id
Inner join (
    SELECT distinct wp_e398be30f4_posts.*
    FROM wp_e398be30f4_posts
    INNER JOIN wp_e398be30f4_postmeta ON (wp_e398be30f4_posts.ID = wp_e398be30f4_postmeta.post_id)
    INNER JOIN wp_e398be30f4_term_relationships ON (wp_e398be30f4_posts.ID = wp_e398be30f4_term_relationships.object_id)
    WHERE 1=1
      AND wp_e398be30f4_posts.post_type = 'offer'
      AND ((wp_e398be30f4_posts.post_status = 'active'))
      AND ((wp_e398be30f4_term_relationships.term_taxonomy_id IN (33)
            AND (0 = 1
                 OR 0 = 1))
           OR (((wp_e398be30f4_postmeta.meta_key = 'title'
                 AND CAST(wp_e398be30f4_postmeta.meta_value AS CHAR) LIKE '%var3%')
                OR (wp_e398be30f4_postmeta.meta_key = 'content'
                    AND CAST(wp_e398be30f4_postmeta.meta_value AS CHAR) LIKE '%var3%'))))
    ) t3 on t1.id = t3.id
    ORDER BY post_date   

答案 1 :(得分:1)

我们最终通过使用它来获得它:

SELECT * FROM
    (select1) as s 
    INNER JOIN (select2) as s2
        USING (ID)
    INNER JOIN (select3) as s3
        USING (ID)
WHERE
    1 = 1

感谢@scaisEdge提出建议:)