【问题标题】:joins on multiple related tables连接多个相关表
【发布时间】:2012-11-01 10:50:02
【问题描述】:

我有 3 个表 customer (customerid,name), customerbooking(bookingid,customerid), transact(transacted,bookingid,typeoftransaction)

我想获取具有最大 typeoftransact='current' 的“客户名称”的名称。客户表通过customerid链接到customerbooking,customerbooking通过bookingid链接到transact。使用 join 我可以获取单个记录,但无法获取最大值

【问题讨论】:

  • 换句话说,您想要拥有“最新”交易记录的客户?

标签: join inner-join correlation correlated-subquery


【解决方案1】:

请尝试这个来满足你的场景

SELECT 
  C.Name
, Count(BookingID) 
FROM Customer C 
INNER JOIN customerbooking CB ON CB.CustomerID = C.customerId
INNER JOIN transact T ON T.bookingid  = CB.BookingId
WHERE T.Typeoftransaction='current'
GROUP BY C.Name

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 1970-01-01
    • 2017-02-08
    • 2014-10-08
    • 2019-08-22
    • 1970-01-01
    相关资源
    最近更新 更多