id 商品id 参数id
1 35 1
2 35 2
3 38 1
4 38 3
5 37 1
以参数id为条件查询商品id,如输入1查询35 37 38 再输入2 需要把1条件累加 查询出 35
本人想法是 自连查询, 请问有什么效率更高的方法,因为如果查询条件过多,会导致自连N次。
1 35 1
2 35 2
3 38 1
4 38 3
5 37 1
以参数id为条件查询商品id,如输入1查询35 37 38 再输入2 需要把1条件累加 查询出 35
本人想法是 自连查询, 请问有什么效率更高的方法,因为如果查询条件过多,会导致自连N次。
select * from table1 where 参数id=1;输入2 需要把1条件累加 查询出 35
select * from table1 where 参数id=2;
我想达到的效果是
select * from table1 as tb1
inner join table1 as tb2
where tb1.参数id = 1 and tb2.参数id = 2最后查出来结果是35
from table1 t
where 参数id = 2
and exists (select 1 from table1 where 参数id = 1)
SELECT a.* from ttp a left join ttp b on a.商品id=b.商品id
where b.参数id=2 and a.参数id=12楼的代码已经可以达到目的
from table1 t
where 参数id = 2
and exists (select 1 from table1 where 商品id=a.商品id and 参数id = 1 )
and exists (select 1 from table1 where 商品id=a.商品id and 参数id = 3 )
and exists (select 1 from table1 where 商品id=a.商品id and 参数id = 4 )
and
exists(select 1 from ttp where a.商品id=商品id and 参数id=3)
....
SELECT * FROM table1WHERE 参数id IN( 1,2) GROUP BY 商品id HAVING COUNT(商品id)=2
count(商品id)=2 2是 in里面参数的个数
这样也可以实现效果。 不知道有什么弊病没。