Mysql
 SQL >> Datenbank >  >> RDS >> Mysql

MySQL – zwei Tabellen ohne Duplikate verknüpfen

Probiere es damit:

SELECT hotels.hotelID, hotels.hotelName, GROUP_CONCAT(operators.opName SEPARATOR ', ') AS opList FROM hotels INNER JOIN operators ON operators.opHotelID = hotels.hotelID GROUP BY(hotels.hotelID) 

Wenn du die Anzahl der Betreiber haben möchtest, musst du COUNT auf die Betreiber-ID anwenden:

SELECT hotels.hotelID, hotels.hotelName, GROUP_CONCAT(operators.opName SEPARATOR ', ') AS opList, COUNT(operators.opID) AS nbOperatos FROM hotels LEFT JOIN operators ON operators.opHotelID = hotels.hotelID GROUP BY(hotels.hotelID)