I have the following SQL (MYSQL Database) query:
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%"
This is doing a great job at returning me a result set that looks like the following:
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
I have tags
table whose schema looks like this id (BIGINT)
, tag_id (BIGINT)
, product_id (BIGINT)
, post_id (BIGINT)
, tag (VARCHAR)
. I would like to join this table so that for the images selected I can also read their respective tag
name from the tags table, but I do not know how to write the correct JOIN to achieve this:
SELECT images.alt, images.product_id, images.src, tags.tag
I am hoping the above statement would return something like:
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
Right now I have something like the following SQL statement and it's not getting me closer to my goal:
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%"
The above statement gets me results that have lots of duplication in it and far from my goal in matching images with their tag name. The results are:
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
How can I write an SQL statement for a MYSQL database that returns the images with their respective tag name?
EDIT 1:
@Dai made a valid comment below.
Your example data output doesn't show how you'd like to get data when
a single product has multiple tags
For a single product that has multiple tags I only want the tags that matches the string of the row's images.alt
data. The alt data follows the following pattern: [color || material] - Swatch
. Tag data follows the following string pattern: [type]:[value]
.
The string comparison should match the [color || material]
part of the image.alt
data and the right side of the colon, the [value]
side, for the tags.tag
data.
See Question&Answers more detail:
os