简体   繁体   English

如何查找列的所有值都相同的行?

[英]How to find rows where all values of a column are same?

I have a table user_test_access which stores test_id and user_id .我有一个表user_test_access存储test_iduser_id

user_test_access table stores all the uses who have access to the test as well as which user created the test. user_test_access表存储有权访问测试的所有用户以及创建测试的用户。

id ID test_creator测试创建者 test_id测试编号 user_id用户身份
1 1个 0 0 1 1个 901 901
2 2个 0 0 1 1个 903 903
3 3个 0 0 2 2个 904 904
4 4个 0 0 2 2个 905 905
5 5个 0 0 3 3个 906 906
6 6个 1 1个 3 3个 907 907
7 7 0 0 3 3个 908 908

I want a query to return all the test_id where there is no creator.我想要一个查询来返回没有创建者的所有test_id ie test_creator = 0.即 test_creator = 0。

Desired Result:期望的结果:

For the particular data set the answer would be test_id 1 and 2. The reason test_id 3 is not included is because user_id 907 is the test_creator for it.对于特定的数据集,答案将是test_id 1 和 2。不包括 test_id 3 的原因是因为user_id 907 是它的test_creator

What I've tried:我试过的:

SELECT test_id from user_test_access WHERE id = ALL(SELECT id from user_test_access WHERE test_creator=0) 

Can you please help me figure out what I'm doing wrong?你能帮我弄清楚我做错了什么吗?

If a missing testcreator is encoded by the value 0 , you can just group by the test_id and select only ids where the sum is zero如果缺少的 testcreator 由值0编码,则可以仅按test_id和 select 仅对总和为零的 ID 进行分组

select test_id 
from user_test_table
group by test_id
having sum(test_creator) = 0

You can use not exists operator as the following:您可以使用 not exists 运算符,如下所示:

SELECT DISTINCT test_id
FROM user_test_access T
WHERE NOT EXISTS (SELECT 1 FROM user_test_access D
  WHERE D.test_id=T.test_id AND D.test_creator=1)

See a demo .看一个演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL-查找值相同或下一个最大值的所有行 - SQL - find all the rows where the values are the same, or next biggest MySQL查找所有行,其中行数(对于列的可能值)小于n - MySQL find all rows where rows with number of rows for possible values of column is less than n 如何选择不同的行,其中一列可能具有许多相同的值,但所有第二列均具有相同的值? - How do I select distinct rows where a column may have a number of the same values but all their 2nd columns have the same value? 如何查找具有所有其他列值的所有行 - How to find all rows that have all another column values 如何获取那些在MySQL中所有值都相同的行 - How to fetch those rows where all values are same in mysql MySql:如何选择所有值都相同的行? - MySql: How to select rows where all values are the same? 选择所有列值相同的行 - Select all rows where the value of column is the same MySQL:仅返回一个表中的行,其中另一个表的一列中的所有值都相同 - MySQL: Return only rows in one table where ALL values in one column of another table are the same 如何在列A具有多个列B的值的情况下查找行(反之亦然) - How to find rows where column A is seen with multiple values of column B (and vice versa) 使用MySQL查找行,其中列的值为'n'且其他值为 - Find rows with MySQL where column has values 'n' AND others values
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM