php - Multiple Where In's in Active Record or SQL -
sorry title of question, wasn't sure how word issue.
i have table this:
entry_id | cat_id ---------+------- 1 | 23 1 | 17 1 | 34 1 | 11 2 | 25 2 | 28 2 | 90 2 | 23
then have different arrays categories
$cats1 = [23, 46, 67] $cats2 = [34, 28, 91]
i need entry_id
's have cat_id
's in both arrays, if do
->where_in('cat_id', $cats1)->where_in('cat_id', $cats2)
obviously doesn't work because no row has more 1 cat_id
associated it. in case, entry_id
want 1
, contains value first array(23)
, 1 second array(34)
.
i can't use or_where
because results not accurate. i'm fine using straight sql select statement, i'm wondering if there way me achieve type of query on table this. i'm not sql pro guidance appreciated.
i've got working using solution entry_id
's first where_in
, have query where_in
id
's , where_in
second array of cats, prefer whole query in 1 shot.
this in php.
(update, correction:) distinct
needed:
select distinct a.entry_id tablex join tablex b on b.entry_id = a.entry_id a.cat_id in (23, 46, 67) , b.cat_id in (34, 28, 91) ;
if have entry
table, use either join
:
select distinct e.entry_id entry e join tablex on a.entry_id = e.entry_id join tablex b on b.entry_id = e.entry_id a.cat_id in (23, 46, 67) , b.cat_id in (34, 28, 91) ;
or exists
correlated subqueries:
select e.entry_id entry e exists ( select * tablex a.entry_id = e.entry_id , a.cat_id in (23, 46, 67) ) , exists ( select * tablex b b.entry_id = e.entry_id , b.cat_id in (34, 28, 91) ) ;
Comments
Post a Comment