簡體   English   中英

如何編寫SQL語句從交叉引用表中獲取匹配數據?

[英]How to write SQL statement to get matching data from a cross-reference table?

我有以下 SQL(MYSQL 數據庫)查詢:

SELECT images.alt, images.product_id, images.src
FROM wp_wps_images images
INNER JOIN wp_wps_products products
ON products.product_id = images.product_id
AND products.product_id IN ("2112055640177","2112056590449","2112055378033","2112062292081","2112058490993","2112062619761","2112062488689","2112066420849","2112061833329","2112052527217")
WHERE images.alt LIKE "%Swatch%"

這在返回給我的結果集方面做得很好,如下所示:

Rust (W) - Swatch   2112058490993   foobar.com
Sand - Swatch   2112058490993   barfoo.com
Tan - Swatch    2112056590449   bazfoo.com
Generic Black - Swatch  2112056590449   tazfoo.com
Patterned / Multi - Swatch  2112055640177   mazfoo.com
Tan - Swatch    2112055640177   bazfoo.com
Generic Black - Swatch  2112055640177   tazfoo.com
Generic Black - Swatch  2112055378033   tazfoo.com
Dark Tobacco - Swatch   2112055378033   makazfoo.com

我有tags表,其架構看起來像這個id (BIGINT) , tag_id (BIGINT) , product_id (BIGINT) , post_id (BIGINT) , tag (VARCHAR) 我想加入這個表,以便對於選擇的圖像,我也可以從標簽表中讀取它們各自的tag名稱,但我不知道如何編寫正確的 JOIN 來實現這一點:

SELECT images.alt, images.product_id, images.src, tags.tag

我希望上面的語句會返回如下內容:

Rust (W) - Swatch   2112058490993   foobar.com           color:rust
Sand - Swatch   2112058490993   barfoo.com               material:sand
Tan - Swatch    2112056590449   bazfoo.com               color:tan
Generic Black - Swatch  2112056590449   tazfoo.com       color:black
Patterned / Multi - Swatch  2112055640177   mazfoo.com   material:multi
Tan - Swatch    2112055640177   bazfoo.com               color:tan
Generic Black - Swatch  2112055640177   tazfoo.com       color:black
Generic Black - Swatch  2112055378033   tazfoo.com       color:black
Dark Tobacco - Swatch   2112055378033   makazfoo.com     color:dark-tobacco

現在我有類似以下 SQL 語句的內容,但它並沒有讓我更接近我的目標:

SELECT images.alt, images.product_id, images.src, tags.tag
FROM wp_wps_images images
INNER JOIN wp_wps_products products
ON products.product_id = images.product_id
AND products.product_id IN ("2112055640177","2112056590449","2112055378033","2112062292081","2112058490993","2112062619761","2112062488689","2112066420849","2112061833329","2112052527217")
INNER JOIN wp_wps_tags tags
ON tags.product_id = products.product_id
AND tags.product_id = images.product_id
AND tags.tag LIKE "%:%"
WHERE images.alt LIKE "%Swatch%"

上面的語句讓我得到的結果中有很多重復,而且與我將圖像與其標簽名稱匹配的目標相去甚遠。 結果是:

Tan - Swatch    2112052527217   bazfoo.com  color:generic-black
Tan - Swatch    2112052527217   bazfoo.com  depth:8
Tan - Swatch    2112052527217   bazfoo.com  height:13
Tan - Swatch    2112052527217   bazfoo.com  material:leather
Tan - Swatch    2112052527217   bazfoo.com  strap:12-1-2
Tan - Swatch    2112052527217   bazfoo.com  style:totes
Tan - Swatch    2112052527217   bazfoo.com  width:19
Generic Black - Swatch  2112052527217   tazfoo.com  color:generic-black
Generic Black - Swatch  2112052527217   tazfoo.com  depth:8

如何為 MYSQL 數據庫編寫 SQL 語句,返回帶有各自標簽名稱的圖像?

編輯 1:

@Dai 在下面發表了有效評論。

您的示例數據輸出未顯示當單個產品具有多個標簽時您希望如何獲取數據

對於具有多個標簽的單個產品,我只想要與行的images.alt數據字符串匹配的標簽。 alt 數據遵循以下模式: [color || material] - Swatch [color || material] - Swatch 標記數據遵循以下字符串模式: [type]:[value]

字符串比較應該匹配[color || material] [color || material]部分image.alt數據和冒號的右側, [value]側,用於tags.tag數據。

你在正確的軌道上 - 兩個內部連接。

問:這個查詢會發生什么:

SELECT images.alt, images.product_id, images.src, tags.tag
FROM wp_wps_images images
INNER JOIN wp_wps_products products
ON products.product_id = images.product_id
INNER JOIN wp_wps_tags tags
ON tags.product_id = images.product_id
WHERE images.alt LIKE "%Swatch%"
AND images.product_id IN ("2112055640177","2112056590449","2112055378033","2112062292081","2112058490993","2112062619761","2112062488689","2112066420849","2112061833329","2112052527217")

換句話說:

1) 只在相互鍵上加入,然后

2) 篩選總體標准

對於表中的數據集:

問:你仍然得到“重復的行”嗎?

問:是否有任何預期的行“缺失”?

問:如果/當單個產品有多行時會發生什么?

請嘗試此查詢並使用結果更新您的帖子。

還:在這里查看更多建議:

SQL 內連接與 3 個表?

SQL INNER JOIN – 從兩個或多個表中查詢數據


  1. 我做了一個額外的改變:我全面使用“images.product_id”。

  2. 它應該為每個像LIKE "%Swatch%"IN ("2112055640177","2112056590449","2112055378033","2112062292081","2112058490993","2112062619761","2112062488689","2112066420849","2112061833329","2112052527217")圖像返回“alt”文本、產品 ID、圖像來源和標簽IN ("2112055640177","2112056590449","2112055378033","2112062292081","2112058490993","2112062619761","2112062488689","2112066420849","2112061833329","2112052527217")

  3. 如果你得到多行......那意味着你有重復的圖像。

  4. 如果標簽為空,則表示該產品 ID 沒有標簽。

  5. 如果您沒有看到您期望的標簽,則意味着沒有與“WHERE”子句匹配的圖像。

  6. 您可以通過復制/粘貼簡單的“SELECT”來驗證 3、4 或 5 中的任何一個。

希望有幫助...

Q:為什么需要加入products表? 你在獲取什么(如果有的話)是“產品”獨有的?

暫無
暫無

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

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