我已经设置了一个php页面,因此有多个数据输入选项可以放入多个临时表中,每个选项都根据数据输入中给定的一个条件查询数据库。因此,如果输入的年龄>10,鞋尺寸>6,高度>60,则会有三个临时表table0、table1和table2,其中table0仅为数据年龄>10、table1仅为数据鞋尺寸>5,而table2仅为数据高度>60。
我想知道如何交叉这些,所以我只会得到年龄>10、鞋码>6、身高>60的所有要求都满足的结果。下面是我使用"WHERE EXISTS"子句的尝试,但它不起作用。
SELECT *
FROM table0 t0
WHERE EXISTS
(SELECT *
FROM table1 t1
WHERE EXISTS
(SELECT *
FROM table2 t2
WHERE t0.age = t1.age = t2.age
AND t0.shoesize = t1.shoesize = t2.shoesize
AND t0.height = t1.height = t2.height));
注意,像这样不依赖表主键的查询会变得很麻烦,所以我建议您添加主键。
也就是说,你需要的查询与你已经尝试过的非常接近:
SELECT *
FROM table0 t0
WHERE EXISTS (
SELECT 1
FROM table1 t1
WHERE t1.age = t0.age AND t1.gender = t0.gender
AND t1.shoesize = t0.shoesize AND t1.weight = t0.weight
AND t1.height = t0.height AND t1.eyes = t0.eyes) AND
EXISTS (
SELECT 1
FROM table2 t2
WHERE t2.age = t0.age AND t2.gender = t0.gender
AND t2.shoesize = t0.shoesize AND t2.weight = t0.weight
AND t2.height = t0.height AND t2.eyes = t0.eyes)
注意:只有当所有值都不为NULL时,上面的查询才会工作。