简体   繁体   English

MySQL-如何联接两个没有重复的表?

[英]MySQL - How to join two tables without duplicates?

I have two tables like the following 我有两个如下表

hotels
------
hotelID
hotelName

Second table 第二张桌子

operators
---------
opID
opName
opServices
opHotelID

a short explanation: In the first table I have a lot of hotels which have an increment id which is unique. 简短的解释:在第一个表中,我有很多旅馆,它们的增量ID是唯一的。 The second table contains all the operators offering this hotel with additional services. 第二个表包含为该酒店提供其他服务的所有运营商。 The opID here is unique too but the opHotelID exists multiple times, because there can be many operators offering the hotel. 这里的opID也是唯一的,但是opHotelID存在多次,因为可能有很多运营商提供该酒店。

Now, what I want to get is the following: 现在,我想得到的是以下内容:

I want to get the HotelName and an additional Column (called Operators) which lists all the operators offering the hotel. 我想获取HotelName和一个附加的列(称为操作员),该列列出了提供酒店的所有操作员。

So the result should be like this... 所以结果应该是这样的...

123 - Hotel ABC - OP1,Op2,OP3

instead of this... 而不是这个...

123 - Hotel ABC - OP1
123 - HOtel ABC - OP2
123 - Hotel ABC - OP3

Is there a way to do this in one SQL query or how would you solve this problem? 有没有办法在一个SQL查询中执行此操作,或者您将如何解决此问题? I am currently working on a search function and currently i have a simple SELECT query with a left join but this returns a lot of more rows. 我目前正在使用搜索功能,目前我有一个带有左联接的简单SELECT查询,但这将返回更多行。 Now the Search should only display unique HotelIDs and combine the different Operators in one column. 现在,搜索应仅显示唯一的HotelID,并将不同的运算符合并到一栏中。

Thanks for you help and have a nice day... 谢谢您的帮助,祝您有美好的一天。

Bye WorldSignia 再见WorldSignia

Try this one: 试试这个:

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)

If you want to have the number of operators, you have to use COUNT on the operators ID like that: 如果要拥有操作员数量,则必须在操作员ID上使用COUNT,如下所示:

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)

您可以使用GROUP_CONCAT

you should have a simple link table, this will create the many to many relationship for many operators to a hotel 您应该有一个简单的链接表,这将为许多经营者与酒店建立多对多关系

operatorhotels
---------
opID
opHotelID

if you dont want to change your DB design, you can use this query 如果您不想更改数据库设计,则可以使用此查询

SELECT hotelID,hotelName,opName FROM hotels h
INNER JOIN operators o ON  h.hotelID = o.opHotelID
GROUP BY h.hotelID,hotelName,opName 

otherwise, create a mapping table resulting the many to many relation 否则,创建一个导致many to many关系的映射表

You should use GROUP_CONCAT as already suggested. 您应该按照建议使用GROUP_CONCAT。 Here's the query: 这是查询:

SELECT h.hotelID, h.hotelName, GROUP_CONCAT(o.opName) 
FROM hotels h
INNER JOIN operators o ON h.hotelID = o.opHotelID
GROUP BY h.hotelID

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

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