Try:
select book_id
from categories
group by book_id
having sum( ( cat_id in (1,3) )::int ) = 2
postgres , (: http://fxjr.blogspot.com/2009/05/npgsql-tips-using-in-queries-with.html), :
select book_id
from categories
group by book_id
having sum( ( cat_id = ANY(ARRAY[1,3]) )::int ) = 2
:
select categories.book_id, books.name
from categories
join books on books.id = categories.book_id
group by categories.book_id
,books.name
having sum( ( categories.cat_id in (1,3) )::int ) = 2
@ , :
ANSI SQL:
select categories.book_id, books.name
from categories
join books on books.id = categories.book_id
group by categories.book_id
,books.name
having count(case when categories.cat_id in (1,3) then 1 end) = 2
:
select book_id
from categories
group by book_id
having count( case when cat_id in (1,3) then 1 end ) = 2
, (.. having) , where having:?...
select book_id
from categories
where category_id in (1,3)
group by book_id
having count(*) = 2
... , having, , 1 3 2 3 4. FTW! , , .
:
select book_id
from categories
group by book_id
having
count( case when cat_id in (1,3) then 1 end ) = 2
or count( case when cat_id in (2,3,4) then 1 end ) = 3
(, , , ), where where:
select book_id
from categories
where cat_id in (1,2,3,4)
group by book_id
having
count( case when cat_id in (1,3) then 1 end ) = 2
or count( case when cat_id in (2,3,4) then 1 end ) = 3
[EDIT]
, MySQL:
select book_id
from categories
group by book_id
having sum( cat_id in (1,3) ) = 2