动态过滤器
问题描述:
TABLE1 (Values)
ID|AUCTION_ID|VALUE_TYPE|VALUE
1|1|726|a
2|1|270|b
3|2|726|a
4|2|270|b
5|3|726|a
6|3|270|b
7|4|983|z
8|4|822|a
TABLE2 (Auctions)
ID|TITLE|DSC|PRICE
1|xxx|xxxxxxxxx|xxx
2|xxx|xxxxxxxxx|xxx
3|xxx|xxxxxxxxx|xxx
4|xxx|xxxxxxxxx|xxx
而现在,如何SELECT * FROM TABLE2其中ID的(从表1中选择AUCTION_ID其中(value_type
= 726和value
= 'a')和(value_type
= 270和value
= 'b')动态过滤器
SO查询效果应该是ID为拍卖是:1和2
HOW TO DO IT BY ONE SQL查询或PHP
PS
此查询正在工作: SELECT ID
FROM table
WHERE value
IN( 'A', 'B', 'C')GROUP BY ID
HAVING COUNT(ID
)> = 3;
BUT THIS暂停SERVER: 从Table_dane选择*,其中(1 = 1)和id
在(SELECT ID
FROM table
WHERE value
IN( 'A', 'B', 'C')GROUP BY ID
HAVING COUNT (ID
)> = 3)
答
我建议exists
。两次:
select t2.*
from TABLE2 t2
where exists (select 1
from Table1 t1
where t2.id = t1.AUCTION_ID and t1.value_type = 726 and t1.value = 'a'
) and
exists (select 1
from Table1 t1
where t2.id = t1.AUCTION_ID and t1.value_type = 270 and t1.value = 'b'
);
出于性能考虑,你想对table1(auction_id, value_type, value)
的索引。