0
    SELECT ITEM_TITLE 
    FROM (SELECT ITEM.ITEM_TITLE
    FROM ITEM JOIN ITEMSELLER ON ITEMSELLER.IS_ITEMID = ITEM.ITEM_ID
    JOIN SELLER ON SELLER.SELLER_ID = ITEMSELLER.IS_SELLERID
    GROUP BY ITEM.ITEM_TITLE
    HAVING COUNT(ITEMSELLER.IS_ITEMID)>1);

对于甲骨文,

我想仅列出已由多个卖家以卖家姓名(所有人)出售的商品。

该代码工作正常,但我无法列出卖家名称,并希望它在卖家数量增加时兼容(这意味着列可以不受限制,取决于有多少卖家出售了相同的商品)。

从上面的代码有3个表,

ITEM : has item_title, item_id

SELLER : has seller_id, seller_fname, seller_lname

ITEMSELLER : has is_itemid (FK with item_id), is_sellerid (FK with sell_id)

结果应该是这样的

items        seller_1      seller_2    seller_3.........seller_x

-------      --------      -------     -------          --------

item_a       John          William     (null)           (null)

item_b       William       Bruce       Julia            (null)

item_c       Julia         John        William          Marry

或者

items        sellers

-------      --------  

item_a       John, William

item_b       William, Bruce, Julia

item_c       Julia, John, William, Marry
4

1 回答 1

1

为此,您可以在 Oracle 中使用LISTAGG ()如下 -

SELECT Item_Title Items, Listagg(Seller.Seller_Fname, ',') Within
 GROUP(
 ORDER BY Seller_Fname)
  FROM Item
  JOIN Itemseller ON Itemseller.Is_Itemid = Item.Item_Id
  JOIN Seller ON Seller.Seller_Id = Itemseller.Is_Sellerid
 GROUP BY Item.Item_Title
HAVING COUNT(Itemseller.Is_Itemid) > 1;
于 2013-05-20T07:59:20.960 回答