select - MySQL Join on two rows -
i'd able rows "articles" table based on 2 "categories" i'm having trouble joins. here's tables like:
`articles` ( `article_id` smallint(5) unsigned not null auto_increment, `article_name` varchar(255) not null primary key (`article_id`) )
`article_categories` ( `article_id` smallint(5) unsigned not null, `category_id` smallint(5) unsigned not null unique key `article_category_id` (`article_id`,`category_id`), key `article_id` (`article_id`), key `category_id` (`category_id`) )
now i'd able articles in both categories 3 , 5 (or unlimited number of categories). thought this:
select * articles inner join article_categories ac on ac.article_id = a.article_id (ac.category_id = 3 , ac.category_id = 5)
just clarify don't want articles in either 3 or 5, both 3 , 5.
i'm thinking 1 of simple things i've somehow missed due tiredness or something.
either or literally have join every category want include e.g:
select a.* articles inner join article_categories ac on ac.article_id = a.article_id inner join article_categories ac2 on ac2.article_id = a.article_id (ac2.category_id = 3 , ac.category_id = 5)
but i'm sure there's simpler solution that.
try
select articles.article_id articles, article_categories articles.article_id=article_categories.article_id , article_categories.category_id in(3,5) group article_categories.article_id having count(*)>=2;
Comments
Post a Comment