gpt4 book ai didi

php - mysql中如何求多个表的交集

转载 作者:行者123 更新时间:2023-11-29 21:09:43 24 4
gpt4 key购买 nike

我已经设置了一个 php 页面,因此有多个数据输入选项可以放入多个临时表中,每个临时表都根据数据输入中给出的 1 个条件查询数据库。所以如果输入是age > 10并且shoesize > 6并且height > 60,就会有三个临时表table0,table1,table2,其中table0只有age > 10的数据,table1只有shoesize > 6的数据,table 2只有数据高度 > 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 时,上面的查询才会起作用。

关于php - mysql中如何求多个表的交集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36490234/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com