简体   繁体   English

MySQL从字段中选择一行,条件是多行

[英]MySQL select one row from field WHERE condition is in multiple rows

Tried to find the answer, but still couldn't.. The table is as follows: 试图找到答案,但仍然找不到。.下表如下:

  name,    room_chat
  Ratna          2
  Ima            2
  Ratna          3
  Yanis          3

i need something like this if i select name from table where (name='Ratna' and name='Ima') i get a result 如果我select name from table where (name='Ratna' and name='Ima')我需要这样的东西select name from table where (name='Ratna' and name='Ima')我得到一个结果

room chat
2

Thanks in advance 提前致谢

在in子句中使用如下:

select distinct a.room_chat from tableA a where a.name in ('Ratna', 'Ima')

You have several rows, but you want to look at a set of rows. 您有几行,但您要查看一组行。 This is called aggregation. 这称为聚合。 In your case you want to look at each room chat, so you group by room_chat . 在您的情况下,您希望查看每个聊天室,因此请group by room_chat You want to know whether for a room chat you find both names, so count the names. 您想知道是否在房间聊天中找到了两个名字,所以计算一下名字。

The query: 查询:

select room_chat
from table 
where name in ('Ratna','Ima')
group by room_chat
having count(distinct name) = 2;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM