拍卖搜索引擎MySQL

时间:2017-04-23 17:05:21

标签: php mysql

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

现在,如何从TABLE2中选择*,其中ID为(从表1中选择AUCTION_ID,其中{value_type = 726和value ='a')AND(value_type = 270并且value ='b')

SO查询效果应该是ID为1的拍卖。

如何通过一个SQL查询或PHP进行操作?

PS。

此查询正在进行中: SELECT ID FROM table WHERE value IN('a','b','c')GROUP BY ID有COUNT(ID)> = 3;

但这会暂停服务器: 从Table_dane中选择*,其中(1 = 1)和id位于(SELECT ID FROM table WHERE value IN('a','b','c') GROUP BY ID有COUNT(ID)> = 3)

1 个答案:

答案 0 :(得分:0)

我建议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)上的索引。