Mysql 选择列具有类似ID的特定值的行';s
我需要编写一个查询,其中我需要返回具有类似ID的行,并且它们需要包含特定的值。在下面的示例中,有多个具有不同颜色的ID 我需要选择所有只包含黄色、红色和蓝色的行。因此,这里0和2的ID将返回这些行,而1和3将不会返回,因为它不包含我需要的特定集合。我还需要返回ID和颜色 身份证件 颜色 0 黄色的 0 红色 0 蓝色 1. 黄色的 1. 红色 2. 黄色的 2. 红色 2. 蓝色 3. 红色 3. 绿色 3. 蓝色Mysql 选择列具有类似ID的特定值的行';s,mysql,Mysql,我需要编写一个查询,其中我需要返回具有类似ID的行,并且它们需要包含特定的值。在下面的示例中,有多个具有不同颜色的ID 我需要选择所有只包含黄色、红色和蓝色的行。因此,这里0和2的ID将返回这些行,而1和3将不会返回,因为它不包含我需要的特定集合。我还需要返回ID和颜色 身份证件 颜色 0 黄色的 0 红色 0 蓝色 1. 黄色的 1. 红色 2. 黄色的 2. 红色 2. 蓝色 3. 红色 3. 绿色 3. 蓝色 下次请与数据共享模式,这样可以节省愿意帮助您的其他人的时间 请注意,这将确保子
下次请与数据共享模式,这样可以节省愿意帮助您的其他人的时间 请注意,这将确保子句中的所有必需值都存在
SELECT id, colors
FROM Test
WHERE id IN (
select id from Test
where colors in ('Yellow','Red', 'Blue')
group by id having count(1) = 3
)
样本数据:
create table Test(id integer, colors varchar(10));
insert into Test(id, colors) values
(0, 'Yellow'), (0, 'Red'), (0, 'Blue'),
(1, 'Yellow'),(1, 'Red'),
(2, 'Yellow'),(2, 'Red'), (2,'Blue'),
(3, 'Red'), (3, 'Green'), (3, 'Blue') ;
SELECT * FROM Test WHERE id IN (select id from Test where colors in ('Yellow','Red', 'Blue') group by id having count(1) = 3) ;
mysql> create database test;
Query OK, 1 row affected (0.00 sec)
mysql> use test;
Database changed
mysql> create table Test(id integer, colors varchar(10));
Query OK, 0 rows affected (0.04 sec)
mysql> insert into Test(id, colors) values
-> (0, 'Yellow'), (0, 'Red'), (0, 'Blue'),
-> (1, 'Yellow'),(1, 'Red'),
-> (2, 'Yellow'),(2, 'Red'), (2,'Blue'),
-> (3, 'Red'), (3, 'Green'), (3, 'Blue') ;
Query OK, 11 rows affected (0.01 sec)
Records: 11 Duplicates: 0 Warnings: 0
mysql> select * from Test;
+------+--------+
| id | colors |
+------+--------+
| 0 | Yellow |
| 0 | Red |
| 0 | Blue |
| 1 | Yellow |
| 1 | Red |
| 2 | Yellow |
| 2 | Red |
| 2 | Blue |
| 3 | Red |
| 3 | Green |
| 3 | Blue |
+------+--------+
11 rows in set (0.00 sec)
mysql> SELECT * FROM Test WHERE id IN (select id from Test where colors in ('Yellow','Red', 'Blue') group by id having count(1) = 3) ;
+------+--------+
| id | colors |
+------+--------+
| 0 | Yellow |
| 0 | Red |
| 0 | Blue |
| 2 | Yellow |
| 2 | Red |
| 2 | Blue |
+------+--------+
6 rows in set (0.00 sec)
mysql> -- For example for Green, Red, Blue
mysql> SELECT * FROM Test WHERE id IN (select id from Test where colors in ('Green','Red', 'Blue') group by id having count(1) = 3) ;
+------+--------+
| id | colors |
+------+--------+
| 3 | Red |
| 3 | Green |
| 3 | Blue |
+------+--------+
3 rows in set (0.00 sec)
测试结果:
create table Test(id integer, colors varchar(10));
insert into Test(id, colors) values
(0, 'Yellow'), (0, 'Red'), (0, 'Blue'),
(1, 'Yellow'),(1, 'Red'),
(2, 'Yellow'),(2, 'Red'), (2,'Blue'),
(3, 'Red'), (3, 'Green'), (3, 'Blue') ;
SELECT * FROM Test WHERE id IN (select id from Test where colors in ('Yellow','Red', 'Blue') group by id having count(1) = 3) ;
mysql> create database test;
Query OK, 1 row affected (0.00 sec)
mysql> use test;
Database changed
mysql> create table Test(id integer, colors varchar(10));
Query OK, 0 rows affected (0.04 sec)
mysql> insert into Test(id, colors) values
-> (0, 'Yellow'), (0, 'Red'), (0, 'Blue'),
-> (1, 'Yellow'),(1, 'Red'),
-> (2, 'Yellow'),(2, 'Red'), (2,'Blue'),
-> (3, 'Red'), (3, 'Green'), (3, 'Blue') ;
Query OK, 11 rows affected (0.01 sec)
Records: 11 Duplicates: 0 Warnings: 0
mysql> select * from Test;
+------+--------+
| id | colors |
+------+--------+
| 0 | Yellow |
| 0 | Red |
| 0 | Blue |
| 1 | Yellow |
| 1 | Red |
| 2 | Yellow |
| 2 | Red |
| 2 | Blue |
| 3 | Red |
| 3 | Green |
| 3 | Blue |
+------+--------+
11 rows in set (0.00 sec)
mysql> SELECT * FROM Test WHERE id IN (select id from Test where colors in ('Yellow','Red', 'Blue') group by id having count(1) = 3) ;
+------+--------+
| id | colors |
+------+--------+
| 0 | Yellow |
| 0 | Red |
| 0 | Blue |
| 2 | Yellow |
| 2 | Red |
| 2 | Blue |
+------+--------+
6 rows in set (0.00 sec)
mysql> -- For example for Green, Red, Blue
mysql> SELECT * FROM Test WHERE id IN (select id from Test where colors in ('Green','Red', 'Blue') group by id having count(1) = 3) ;
+------+--------+
| id | colors |
+------+--------+
| 3 | Red |
| 3 | Green |
| 3 | Blue |
+------+--------+
3 rows in set (0.00 sec)
您只有两行ID=2?您希望如何在输出中返回3行?@Akshay我刚刚编辑了这个问题。糟糕的是,我漏掉了那一行。这对数据集做出了隐含的假设,虽然这些假设可能是正确的,但应该做出explicit@strawberry你说得对,我同意,谢谢你的更正