如何在 SQL 中显示来自 3 个不同表的多个列?

Posted

技术标签:

【中文标题】如何在 SQL 中显示来自 3 个不同表的多个列?【英文标题】:How do I display multiple columns from 3 Different tables in SQL? 【发布时间】:2020-03-17 05:49:59 【问题描述】:

我有 3 张桌子:

ReservationTripCustomer

我只需要显示多次预订的客户的行程名称、行程类型、客户名字、客户姓氏

有点像

预订表

Reservation_ID ... ... .. Customer_Num

    16001 101 16002 101 16003 102 16004 103 16005 103

客户表

Customer_ID ... ... .. Customer_Num

    30 101 31 102 32 103

客户表的主 ID 是 customer_ID,列名是 Customer_Num。此列名也在reservation 表中。

预留表的主 ID 是 Reservation_ID

我试过了:

SELECT Customer.First_Name, Customer.Last_Name, Trip.Trip_Name, Trip.Type, Reservation.Customer_Num COUNT(Reservation.Customer_Num
FROM Reservation, Customer, Trip
WHERE Reservation.Customer_Num = Customer.Customer_Num
HAVING COUNT(Reservation.Customer_Num) > 1
GROUP BY Customer.First_Name, Customer.Last_Name, Trip.Trip_Name, Trip.Type, Reservation.Customer_Num;

【问题讨论】:

请分享示例数据和预期结果。 嗨@MTcodes,Trip 表在哪里?另外,请向我们展示您迄今为止尝试过的代码? 【参考方案1】:

试试这个,假设 Trip 表使用 Customer_num 字段作为外键

select A.Customer_id, A.Name, A.Last_name, B.name, B.type from Customer as A
where Customer_id in 
    (select Customer_Num from Reservation group by Customer_Num having count(Customer_Num) > 1)
left join Trip as B on Trip.Customer_Num = Customer.Customer_Num

【讨论】:

【参考方案2】:

你可以使用EXISTS

SELECT t.*, c.*
FROM reservation r INNER JOIN
     customer c
     ON c.Customer_Num = r.Customer_Num INNER JOIN
     trip t
     ON . . .
WHERE EXISTS (SELECT 1 
              FROM reservation r1 
              WHERE r1.customer_no = r.customer_no AND 
                    r1.Reservation_ID <> r.Reservation_ID 
             );

Trip表的ON子句你没有指定表信息,需要调整。

【讨论】:

【参考方案3】:

假设您的 TRIP 表中包含旅行名称、旅行类型等列,您可以尝试以下查询 -

SELECT Customer.First_Name
      ,Customer.Last_Name
      ,Trip.Trip_Name
      ,Trip.Type
      ,Reservation.Customer_Num
      ,COUNT(Reservation.Customer_Num) Customer_Num_Cnt
FROM Reservation R
INNER JOIN Customer C ON Reservation.Customer_Num = Customer.Customer_Num
INNER JOIN Trip T ON R.TRIP_ID = T.TRIP_ID
GROUP BY Customer.First_Name
        ,Customer.Last_Name
        ,Trip.Trip_Name
        ,Trip.Type
        ,Reservation.Customer_Num
-- HAVING COUNT(Reservation.Customer_Num) > 1;

如果这不能满足您的要求,请分享 TRIP 表和 CUSTOMER 表的完整结构。

【讨论】:

我添加了行程表以及其他两个表,只是为了澄清。 使用了代码,但必须删除每个表名后的“C”“R”和“T”,只使用完整的表名。它说没有找到数据 SELECT CUSTOMER.FIRST_NAME, CUSTOMER.LAST_NAME, TRIP.TRIP_NAME, TRIP.TYPE, RESERVATION.CUSTOMER_NUM, COUNT(RESERVATION.CUSTOMER_NUM) CUSTOMER_NUM_CNT FROM RESERVATION INNER JOIN CUSTOMER ON RESERVATION.CUSTOMER_NUM = CUSTOMER.CUSTOMER_NUM INNER JOIN TRIP ON RESERVATION.TRIP_ID = TRIP.TRIP_ID GROUP BY CUSTOMER.FIRST_NAME, CUSTOMER.LAST_NAME, TRIP.TRIP_NAME, TRIP.TYPE, RESERVATION.CUSTOMER_NUM HAVING COUNT(RESERVATION.CUSTOMER_NUM) > 1; HAVING 条件可能会过滤所有记录,因为可能没有客户在 Reservation 表中拥有超过 2 个条目。只需删除 HAVING 子句并重试。为了您的方便,我更新了查询以注释 Have 子句。 感谢您的帮助!!【参考方案4】:

这会显示结果,以便我可以选择哪些名称有多个预订。在预订表中,它列出了有多个预订的客户编号,但我不确定为什么它不起作用。

SELECT CUSTOMER.FIRST_NAME, CUSTOMER.LAST_NAME, TRIP.TRIP_NAME, TRIP.TYPE, RESERVATION.CUSTOMER_NUM, COUNT(RESERVATION.CUSTOMER_NUM) CUSTOMER_NUM_CNT

来自预订

在 CUSTOMER.CUSTOMER_NUM = RESERVATION.CUSTOMER_NUM 上的内部加入客户 INNER JOIN TRIP ON RESERVATION.TRIP_ID = TRIP.TRIP_ID

GROUP BY CUSTOMER.FIRST_NAME、CUSTOMER.LAST_NAME、TRIP.TRIP_NAME、TRIP.TYPE、RESERVATION.CUSTOMER_NUM 注释掉--HAVING COUNT(RESERVATION.CUSTOMER_NUM) > 1;

【讨论】:

以上是关于如何在 SQL 中显示来自 3 个不同表的多个列?的主要内容,如果未能解决你的问题,请参考以下文章

如何在sql中连接来自不同表的两个字段

在sql中组合来自不同表的数据

在oracle中显示来自多个表的某些列的数据不起作用

DAX Measure 在过滤器表达式中使用来自不同相关表的多个列

如何使用 linq2sql 在单个 gridview 或 gridpanel 中显示来自多个表的数据?

如何计算来自不同datagridview的每列总和之间的差异并将其显示在datagridview中