我在 sql 中有一个表,我想通过以下方式检索基于两列的数据 传递两个值列表。 例如:- 让我们考虑一个表名为“employee”的表
-----------------------------
| project_id | resource_id |
-----------------------------
| 7 | 46 |
-----------------------------
| 7 | 87 |
-----------------------------
| 5 | 87 |
------------------------------
我想根据两个值的精确匹配来检索数据 列就像
"select * from employee where project_id = 7 and resource_id = 46;"
然后它返回第一行。如果查询是这样的
"select * from employee where project_id in (7,5) and resource_id in
(46,87);"
它将返回所有三行而不是第一行和第三行。 我可以理解“sql”引擎通过所有 给定值的组合。但是,我需要查询来处理数据 对于上述值,以返回第一行和第三行。有没有 解决方案? 提前致谢。
最佳答案
如果您的 dbms 支持行类型等:
select *
from employee
where (project_id, resource_id) in ((7, 46), (5, 87))
ISO/ANSI SQL 合规性:
The following features outside Core SQL-2003 are used:
F641, "Row and table constructors"
T051, "Row types"
F561, "Full value expressions"
关于SQL通过将两个列表传递给两列来检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43870765/