gpt4 book ai didi

mysql - 具有值的返回行存在于两行中

转载 作者:行者123 更新时间:2023-11-29 02:30:11 25 4
gpt4 key购买 nike

+------+------+------------+------+
| id | type| design| location |
+------+------+------------+------+
| 1 | abc | A | Location X |
| 2 | abc | A | Location Y |
| 3 | def | A | Location X |
+------+------+------------+------+

我有上面的表格。

假设我想获取位置 X 和 Y 中存在的类型。它应该返回第 1 行和第 2 行。

我应该使用什么sql?

最佳答案

SELECT type
FROM tableName
WHERE location IN ('location x','location y')
GROUP BY type
HAVING COUNT(*) = 2

或者如果您想拥有所有这些值,请使用 JOIn

SELECT  a.*
FROM tableName a
INNER JOIN
(
SELECT type
FROM tableName
WHERE location IN ('location x','location y')
GROUP BY type
HAVING COUNT(*) = 2
) b ON a.type = b.type

来源

关于mysql - 具有值的返回行存在于两行中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14474998/

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