简体   繁体   中英

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

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 . 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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