2
我有以下表結構:Mysql的選擇行
item_id | value | ================== 1 | 1 | 1 | 3 | 1 | 4 | 2 | 2 | 2 | 3 | 2 | 4 | 2 | 5 | 3 | 1 | 3 | 5 | 3 | 6 | 4 | 1 | 4 | 3 | 4 | 4 | 4 | 5 |
我有一個返回那些ITEM_ID其值與1,3和4 因此,這裏相匹配的查詢時,應該返回ITEM_IDS是1和4
我的查詢:
select item_id from table t where exists (select item_id from table t1 where value = 1 and t1.item_id = t.item_id) and exists (select item_id from table t1 where value = 2 and t1.item_id = t.item_id) group by item_id
此查詢工作正常。在這裏,我只匹配3個值。如果我想從表中匹配50個這樣的值,該怎麼辦? (所有的50個值都存儲在一個php數組中)查詢將是巨大的,我也想從同一個查詢中的兩個不同表中做同樣的事情。所以,這將會使已經很大的查詢變大一倍。請給我建議一些其他的方法。
編輯::
table 2 -------- item_id | user_id | ================== 1 | 1 | 1 | 5 | 1 | 7 | 2 | 2 | 2 | 3 | 2 | 4 | 2 | 5 | 3 | 1 | 3 | 5 | 3 | 6 | 4 | 1 | 4 | 3 | 4 | 4 | 4 | 5 |
現在,我想ITEM_ID其中來自表1的值是1,3,4和USER_ID從表2是1,5,7
謝謝。這節省了我的日子 – 2013-05-14 06:40:44
':)' – 2013-05-14 06:40:58
+1 ..我結束了與urs相同的'有'查詢...如此刪除:) – Kshitij 2013-05-14 06:47:24