选择匹配列表中所有项目的一组行

编程入门 行业动态 更新时间:2024-10-24 21:28:22
本文介绍了选择匹配列表中所有项目的一组行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我有两个表:

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 )

更多推荐

选择匹配列表中所有项目的一组行

本文发布于:2023-10-26 23:17:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531637.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:项目   列表中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!