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);
对于Oracle,
我想仅列出多个卖家以卖家姓名(每个人)出售的商品。
代码工作正常,但我不能列出卖家名称,并希望它在卖家数量增加时兼容(意味着列数可以无限制取决于卖家销售同一商品的数量)。
从上面的代码中有3个表,
ITEM : has item_title, item_id
SELLER : has seller_id, seller_fname, seller_lname
ITEMSELLER:有is_itemid(带有item_id的FK),is_sellerid(带有seller_id的FK)
结果应该是这样的
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
答案 0 :(得分: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;