簡體   English   中英

將一個表中的兩列連接到其他表中的一列,則兩列中的每一個都返回不同或相同的結果

[英]Joining two columns from one table to one column in other returning different or the same result for each of the 2 columns

我有4張桌子,可以輕松加入。 最后一個聯接要求將兩列聯接到一個類別表中。 我想要在同一行中返回不同的值(categoryName)。

期望的輸出

customerID,adID,categoryselectedID,officecategoryID,categoryID,categoryName(這是正確的),wantcategoryID,categoryID,categoryName(這與顯示的名稱相同)

表和列

  1. 客戶:customerID(PK)
  2. 廣告:adID(PK),customerID(FK)
  3. 所選類別:categoryselectedID(PK),adID(FK),提供的categoryID(FK),想要的categoryID(FK)
  4. 類別:categoryID(PK),categoryName

(mysql返回7條記錄,除categoryName之外,所有信息均與提供和需求相同)

SELECT customers.*, ads.*, categoriesselected.categoryselectedID, categoriesselected.offeredcategoryID, offeredcategory.categoryID, offeredcategory.categoryName, categoriesselected.wantedcategoryID, wantedcategory.categoryID, wantedcategory.categoryName

FROM customers
    INNER JOIN ads ON ads.customerId = customers.customerID
    INNER JOIN categoriesselected ON categoriesselected.adID = ads.adID 
    LEFT OUTER JOIN categories AS offeredcategory ON offeredcategory.categoryID = categoriesselected.offeredcategoryID
    LEFT OUTER JOIN categories AS wantedcategory ON wantedcategory.categoryID = categoriesselected.offeredcategoryID

(我還嘗試了UNION和UNION都返回14條記錄7,以提供7種想要的類別,而不是只有7條記錄在同一行上具有不同的categoryName。)

SELECT customers.*, ads.*, categoriesselected.categoryselectedID, categoriesselected.offeredcategoryID, categories.categoryID, categories.categoryName,  'Offered Category' AS Category 
FROM customers
    JOIN ads ON ads.customerId = customers.customerID
    JOIN categoriesselected ON categoriesselected.adID = ads.adID 
    JOIN categories ON categories.categoryID = categoriesselected.offeredcategoryID

UNION ALL

SELECT customers.*, ads.*, categoriesselected.categoryselectedID, categoriesselected.wantedcategoryID, categories.categoryID, categories.categoryName,  'Wanted Category' AS Category 
FROM customers
    JOIN ads ON ads.customerId = customers.customerID
    JOIN categoriesselected ON categoriesselected.adID = ads.adID 
    JOIN categories ON categories.categoryID = categoriesselected.wantedcategoryID

如果我沒記錯的話,問題就出在最后一個聯接上,這給了你錯誤的輸出。

SELECT customers.*, ads.*, categoriesselected.categoryselectedID, 
    categoriesselected.offeredcategoryID, offeredcategory.categoryID, offeredcategory.categoryName, 
    categoriesselected.wantedcategoryID, wantedcategory.categoryID, wantedcategory.categoryName
FROM customers
    INNER JOIN ads ON ads.customerId = customers.customerID
    INNER JOIN categoriesselected ON categoriesselected.adID = ads.adID 
    LEFT OUTER JOIN categories AS offeredcategory ON offeredcategory.categoryID = categoriesselected.offeredcategoryID
    LEFT OUTER JOIN categories AS wantedcategory ON wantedcategory.categoryID = categoriesselected.wantedcategoryID

請注意,我改變了聯接查詢條件從offeredcategoryIDwantedcategoryIDwantedcategory

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM