在2个查询的联合中获得问题

时间:2016-10-17 14:06:54

标签: mysql join union

我在下面的查询中遇到了问题 -

SELECT DISTINCT id
FROM wp_posts
INNER JOIN wp_postcodes ON (
        wp_postcodes.post_id = wp_posts.id
        AND wp_posts.post_type = 'listing'
        AND TRUNCATE (
            (degrees(acos(sin(radians(latitude)) * sin(radians('37.090240')) + cos(radians(latitude)) * cos(radians('37.090240')) * cos(radians(longitude - '-95.712891')))) * 69.09),
            1
            ) <= 400
        AND TRUNCATE (
            (degrees(acos(sin(radians(latitude)) * sin(radians('37.090240')) + cos(radians(latitude)) * cos(radians('37.090240')) * cos(radians(longitude - '-95.712891')))) * 69.09),
            1
            ) >= 40
        )
INNER JOIN wp_postmeta ON (
        wp_postmeta.post_id = wp_posts.id
        AND wp_posts.post_type = 'listing'  
        AND wp_posts.post_status = 'publish'
        AND (   
            (
                meta_key = 'city'
                AND meta_value = 'sports'
                )
            OR (
                meta_key = 'state'
                AND meta_value = 'sports'
                )
            OR post_title = 'sports'
            )
        )
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_terms ON (wp_terms.term_id = wp_term_relationships.term_taxonomy_id)
INNER JOIN wp_term_taxonomy ON (
        wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
        AND wp_term_taxonomy.taxonomy = 'listingcategory'
        AND wp_term_taxonomy.term_id = 406
        )

UNION

    DISTINCT
SELECT DISTINCT id
FROM wp_posts
INNER JOIN wp_postcodes ON (
        wp_postcodes.post_id = wp_posts.id
        AND wp_posts.post_type = 'listing'
        AND TRUNCATE (
            (degrees(acos(sin(radians(latitude)) * sin(radians('37.090240')) + cos(radians(latitude)) * cos(radians('37.090240')) * cos(radians(longitude - '-95.712891')))) * 69.09),
            1
            ) <= 400
        AND TRUNCATE (
            (degrees(acos(sin(radians(latitude)) * sin(radians('37.090240')) + cos(radians(latitude)) * cos(radians('37.090240')) * cos(radians(longitude - '-95.712891')))) * 69.09),
            1
            ) >= 40
        )
INNER JOIN wp_postmeta ON (
        wp_postmeta.post_id = wp_posts.id
        AND wp_posts.post_type = 'listing'
        AND wp_posts.post_status = 'publish'
        AND (
            (
                meta_key = 'city'
                AND meta_value LIKE '%sports%'
                )
            OR (
                meta_key = 'state'
                AND meta_value LIKE '%sports%'
                )
            OR post_title LIKE '%sports%'
            )
        )
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_terms ON (wp_terms.term_id = wp_term_relationships.term_taxonomy_id)
INNER JOIN wp_term_taxonomy ON (
        wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
        AND wp_term_taxonomy.taxonomy = 'listingcategory'
        AND wp_term_taxonomy.term_id = 406
        )

这里有2个带union的查询,1st没有输出但是第2个获得输出。我正在执行两个查询的联合。这应该是一个结果 但我没有结果。请说明我在做什么错误..

2 个答案:

答案 0 :(得分:0)

UNION操作尝试合并结果集并删除重复项。尝试UNION ALL并查看

答案 1 :(得分:0)

感谢您的回复。 我已经解决了我的问题,这里UNION对我来说工作正常而不是UNION DISTINCT。现在我的担忧已经解决了。