簡體   English   中英

如何有條件地從不同表的兩列中選擇

[英]How to select from two columns in different tables conditionally

我有兩張桌子

1. products <id, taxon, .... >
2. tags <id, tag_name, product_id, value, ....>

我想在這兩個表上聯接(或執行一些其他操作)以獲得分類,但是基於特定tag_name的值。 讓我更明確地說,如果產品的標簽具有tag_name=typevalue=precious ,那么我想將此標簽值視為一個分類單元。

因此,如果我有以下條目:

<# product id:1, taxon:"Jewellery">
<# product id:2, taxon:"Apparel">
<tag id:1, tag_name:"type", product_id:1, value="Precious">

我想要結果表中的以下內容

product_id   taxon_name
    1        Jewellery
    1        Precious
    2        Apparel

如何形成這樣的查詢?

謝謝

這很棘手...

一個UNION可能會這樣做。 看看結果如何:

SELECT id,taxon 
 FROM products 
 UNION ALL (
     select product_id as id,value as taxon 
       from tags 
       where tag_name='type' and value='Precious'
  );

基本上,這會從產品中加載idtaxon ,然后(以某種方式)將tags表中的兩個相關列附加到末尾。

暫無
暫無

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

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