Tsitna FZ Tsitna FZ - 11 months ago 49
MySQL Question

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

Thanks in advance

Answer Source

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;