简体   繁体   English

SQL查询从多个表返回数据?

[英]SQL query to return data from multiple tables?

I have tables with the following schemas: 我有具有以下模式的表:

items (itemID: integer, description: string, price: integer)
orders (orderID: integer, itemID: integer, aID: integer, customerID: integer, date: date)

I would like to find out each item for which there is only one order, return the itemID, description, and the date of the order. 我想找出只有一个订单的每个项目,返回itemID,描述和订单日期。

I have the following code so far: 到目前为止,我有以下代码:

SELECT *
FROM (
SELECT *
FROM orders
GROUP BY itemID
HAVING COUNT(*) = 1
) AS ONLY_ONCE

But this returns only the information from the orders table (orderID, itemID, aID, and customerID) in my code I search for items for which the itemID appears only once in the orders table, which means that it was only ordered once. 但是,这仅返回我的代码中订单表中的信息(orderID,itemID,aID和customerID),我在订单表中搜索针对其itemID仅出现一次的商品,这意味着该商品仅被订购了一次。 How do I get the description of these items which is in the items table? 如何获得项目表中这些项目的描述? I tried using the join function to do this but was not successful. 我尝试使用join函数执行此操作,但未成功。

Thanks 谢谢

You were very close... try this: 您距离很近...试试这个:

SELECT *
FROM items
WHERE itemID IN (
    SELECT itemID
    FROM orders
    GROUP BY itemID
    HAVING COUNT(*) = 1
) 

If you also want the date from orders in your result set, there are many ways to do that, but I'd opt for a CTE and an inner join over the IN operator, like so: 如果您还希望从结果集中的订单中获取日期,可以采用多种方法,但是我会选择CTE和IN运算符上的内部联接,例如:

;WITH SingleOrders AS (
    SELECT itemID, MAX([date]) AS [Date]
    FROM orders
    GROUP BY itemID
    HAVING COUNT(*) = 1
) SELECT *
    FROM items i
         INNER JOIN SingleOrders so ON i.itemID = so.itemID

The first part declares a common table expression called "SingleOrders" which consists of two columns: the ID and the Date for each itemID that has only one order. 第一部分声明了一个公用表表达式“ SingleOrders”,该表表达式由两列组成:每个itemID的ID和Date仅具有一个顺序。 The MAX(date) returns the one and only date, and is required because of the GROUP BY clause. MAX(date)返回一个唯一的日期,并且由于GROUP BY子句而必须使用。

The actual SELECT statement then joins this CTE with the items table to select out only the itemIDs that have just one order, and since it includes all the columns from both the items and the SingleOrders tables, will include the date. 然后,实际的SELECT语句将此CTE与items表连接在一起,以仅选择仅具有一个订单的itemID,并且由于它包括item和SingleOrders表中的所有列,因此将包括日期。

If you don't want to use a CTE (or can't use one), something like this might work for you... just add an inner join back to the orders table to pick up the date: 如果您不想使用CTE(或不能使用CTE),则可能适合您……仅向订单表添加内部联接以选择日期:

SELECT i.*, o.date
FROM items i
     INNER JOIN orders o ON i.itemID = o.itemID
WHERE i.itemID IN (
    SELECT itemID
    FROM orders
    GROUP BY itemID
    HAVING COUNT(*) = 1
) 

You need to join your 'group' table result with the original data 您需要将“组”表结果与原始数据结合在一起

SELECT I.*
FROM items I
INNER JOIN(
    SELECT itemID
    FROM orders
    GROUP BY itemID HAVING COUNT(1)=1
    ) O ON O.itemID=I.itemID

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

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