Intersection in mysql
my first query
SELECT ID FROM wp_posts
WHERE post_type = 'post' AND post_status = 'publish'
AND ID IN (Select object_id FROM wp_term_relationships, wp_terms
WHERE wp_term_relationships.term_taxonomy_id =18开发者_运维技巧)
ORDER BY post_date DESC
produce result
31 and 28
and my second query
SELECT ID FROM wp_posts
WHERE post_type = 'post' AND post_status = 'publish'
AND ID IN (Select object_id FROM wp_term_relationships, wp_terms
WHERE wp_term_relationships.term_taxonomy_id =8)
ORDER BY post_date DESC
will produce
31 and 33
I need to get intersection of both query result.ie, 31
only. how can we get it using mysql
An intersect is nothing more than an inner join, actually:
select
a.ID as ID
from
(SELECT ID FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' AND ID IN (Select object_id FROM wp_term_relationships, wp_terms WHERE wp_term_relationships.term_taxonomy_id =8 or wp_term_relationships.term_taxonomy_id =18)) as a,
(SELECT ID FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' AND ID IN (Select object_id FROM wp_term_relationships, wp_terms WHERE wp_term_relationships.term_taxonomy_id =18 or wp_term_relationships.term_taxonomy_id =18)) as b
where
a.ID=b.ID
From the top of my head. I just copy & pasted your queries, btw.
One simple way would be like this:
SELECT FROM (that entire first query) WHERE ID IN (that entire second query)
I make no promises about its efficiency...
Your query is somehow not clear. Look at the second query. You have the following
wp_term_relationships.term_taxonomy_id =18 OR
wp_term_relationships.term_taxonomy_id =18
hmmm intersection - try inner join :P
精彩评论