I have a 3NF design (Tag , Item_Tag, Item)
As below but it wont work because i'm looping the same old cloumn multiple times.
I'm trying to Achive a list of tag with AND CONDITION. On OR condition , I Just have to do a whereIn but with a AND condition, I'm lost on how I can actually go head with it.
*More explanation*
ItemID TagID
203 45
203 60
203 61
203 62
204 45
204 50
204 51
204 52
Let's say I'm trying to get all ItemID Where Tag = 45 and tag = 60(And Condition)
This is my currently code and it wont work.
$itemID = DB::table('item_tag')
->where(function($query) use($ID)
{
foreach($ID as $uID){
$query->where('tag_id', '=', $uID);
}
})->lists('item_id');
Potential Solution 1
select * from question q inner join
question_has_tag qt where tag_id in
(select tag_id from tags where (what we want)
minus select tag_id from tags where (what we don't)
Potential Solution 2
SELECT * FROM items WHERE
EXISTS (SELECT 1 FROM item_tag WHERE id = item_id AND tag_id = tag1)
AND EXISTS (SELECT 1 FROM item_tag WHERE id = item_id AND tag_id = tag2)
AND ...
Is this possible & how can I can do Solution 1 & 2 with Laravel 4. Thanks in advance.
select * from question q
inner join question_has_tag qt
ON ...
where tag_id in (select tag_id from tags where (what we want))
AND tag_id NOT IN (select tag_id from tags where (what we don't))
I think this is the logic you are looking for, note the missing ON clause.
If the tables are indexed properly you will probably be better off creating a #tmp table for (select tag_id from tags where (what we want))
and using inner joins rather than sub-queries.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.