开发者

Mysql Inner Join Issues

This gives me all my articles that contain both the mentioned entities.

SELECT COUNT(ArticlesEntity.article_id) AS article_count
FROM articles_entities ArticlesEntity 
WHERE ArticlesEntity.entity_id IN ('ENTITY_ID_1','ENTITY_ID_2') 
GROUP BY ArticlesEntity.article_id 
HAVING article_count>=2 

I now want to add something to this query that excludes any entity that has 'ENTITY_ID_3'.

I tried the following but it returned the same results:

SELECT COU开发者_JS百科NT(ArticlesEntity.article_id) AS article_count
FROM articles_entities ArticlesEntity 
WHERE ArticlesEntity.entity_id IN ('ENTITY_ID_1','ENTITY_ID_2') 
AND ArticlesEntity.entity_id NOT IN ('ENTITY_ID_3')
GROUP BY ArticlesEntity.article_id 
HAVING article_count>=2 

What am I missing / doing wrong?

Thanks in advance


Try something like:

SELECT COUNT(ArticlesEntity.article_id) AS article_count 
FROM articles_entities ArticlesEntity  
WHERE ArticlesEntity.entity_id IN ('ENTITY_ID_1','ENTITY_ID_2')  
    AND NOT EXISTS (
        select 1 
        from articles_entities 
        where article_id = ArticlesEntity.article_id 
            and entity_id = 'ENTITY_ID_3')
GROUP BY ArticlesEntity.article_id  
HAVING article_count>=2  


There must be more to your query than what you're showing here, because from what you're showing in your question, the results should be identical for both queries (If entity_id is equal to either id_1 or id_2, it's already not equal to id_3).

Edit: Sorry, just noticed two things. 1) in your problem statement you said the results are identical (as expected). 2) in your title, you say "Inner Join Issues", but... you ain't got no inner join.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜