我有以下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
数据的字符串匹配的标签。替代数据遵循以下模式:[color || material] - Swatch
。标签数据遵循以下字符串模式:[type]:[value]
。
字符串比较应与[color || material]
数据的image.alt
数据的[value]
部分和冒号的右侧tags.tag
匹配。
答案 0 :(得分:1)
您在正确的轨道上-两个内部联接。
问:此查询会发生什么:
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)过滤总体标准
对于表中的数据集:
问:您还会得到“重复的行”吗?
问:是否有任何预期的行“丢失”?
问:如果/当一个产品有多行时会发生什么?
请尝试执行此查询并使用结果更新您的帖子。
ALSO:在此处查找更多建议:
SQL INNER JOIN – querying data from two or more tables
我进行了另一项更改:我正在全面使用“ images.product_id”。
对于每个LIKE "%Swatch%"
和IN ("2112055640177","2112056590449","2112055378033","2112062292081","2112058490993","2112062619761","2112062488689","2112066420849","2112061833329","2112052527217")
的图像,它应该返回“ alt”文本,产品ID,图像来源和标签。
如果您有多行...表示您有重复的图像。
如果标签为空,则表示该产品ID没有标签。
如果您没有看到期望的标签,则意味着没有与“ WHERE”子句匹配的图像。
您可以通过复制/粘贴简单的“ SELECT”来验证3、4或5中的任何一个。
希望有帮助...
问:为什么需要加入产品表?您要获取“产品”特有的什么(如果有的话)?