MySQL Merge一起选择吗?
要将选择合并在一起,您需要使用GROUPBY子句。为了理解这个概念,让我们创建一个表。创建表的查询如下-
mysql> create table MergingSelectDemo -> ( -> RoomServicesId int, -> RoomId int, -> ServiceId int -> );
现在,您可以使用insert命令在表中插入一些记录。查询如下-
mysql> insert into MergingSelectDemo values(10,10,10); mysql> insert into MergingSelectDemo values(20,10,20); mysql> insert into MergingSelectDemo values(30,10,30); mysql> insert into MergingSelectDemo values(50,10,50); mysql> insert into MergingSelectDemo values(110,20,20); mysql> insert into MergingSelectDemo values(120,20,30);
使用select语句显示表中的所有记录。查询如下-
mysql> select *from MergingSelectDemo;
以下是输出-
+----------------+--------+-----------+ | RoomServicesId | RoomId | ServiceId | +----------------+--------+-----------+ | 10 | 10 | 10 | | 20 | 10 | 20 | | 30 | 10 | 30 | | 50 | 10 | 50 | | 110 | 20 | 20 | | 120 | 20 | 30 | +----------------+--------+-----------+ 6 rows in set (0.00 sec)
这是将选择合并在一起的查询-
mysql> select RoomId from MergingSelectDemo -> where ServiceId IN(10,20,30) -> group by RoomId -> HAVING COUNT(*)=3;
以下是输出-
+--------+ | RoomId | +--------+ | 10 | +--------+ 1 row in set (0.15 sec)