简体   繁体   中英

GROUP BY all columns?

New to MySQL, couldn't find the right answer here.

I have these tables: Passengers, and Tickets. I need to show all the data of the passengers that bought 5 tickets, no just the ID

I've managed to do this, but I need all the data of the passengers.

SELECT Passenger.IdPassenger
FROM Passengers
JOIN Ticket ON Passenger.IdPassenger = Ticket.IdPassenger
GROUP BY Passenger.IdPassenger
HAVING COUNT(*) = 5;

The only way mysql let me show all the data of the passengers is doing SELECT * and then in the GROUP BY clause put EVERY column, is there a way to do a group by all like group by * ?

Thank you.

You can use id's in subquery to get all data :

select *
  from Passengers p
 where p.IdPassenger in (select Ticket.IdPassenger
                           from Ticket
                          group by Ticket.IdPassenger
                         having count(*) = 5)

GROUP BY the Tickets table in a derived table , to get 5 tickets id's. JOIN :

select p.*
from Passengers p
join (SELECT IdPassenger
      FROM Ticket
      GROUP BY IdPassenger
      HAVING COUNT(*) = 5) t
   ON p.IdPassenger = t.IdPassenger

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