Select group of rows that match all items in a list(选择与列表中的所有项目匹配的行组)
问题描述
假设我有两张桌子:
cars – 汽车列表
carname | modelnumber | ...
passedtest – 包含汽车通过的所有测试:
passedtest – contains every test that a car passed:
id | carname | testtype | date | ...
1 | carA | A | 2000 |
2 | carB | C | 2000 |
3 | carC | D | 2001 |
4 | carA | C | 2002 |
现在,我如何从 passedtest 表中选择通过所有测试(A、B、C、D)的汽车?
Now, how can I select a car from the passedtest table that passed all tests (A, B, C, D)?
我尝试了 IN 语句,但它也匹配通过了一项测试的汽车.我正在寻找一个语句来匹配列表中所有行的 all 值.
I tried the IN statement but it also matches cars that pass even one test. I am looking for a statement to match all values in a list across all rows.
推荐答案
这个怎么样?
SELECT carname
FROM PassedTest
GROUP BY carname
HAVING COUNT(DISTINCT testtype) = 4
您也可以将它用作从 cars 表中获取信息的内部语句:
You can also use it as an inner statement for taking info from the cars table:
SELECT *
FROM cars
WHERE carname IN (
SELECT carname
FROM PassedTest
GROUP BY carname
HAVING COUNT(DISTINCT testtype) = 4
)
这篇关于选择与列表中的所有项目匹配的行组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:选择与列表中的所有项目匹配的行组
基础教程推荐
- 在 SQL 中连接多个表 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 无法解决整理冲突 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
