sql - Column names based on a mapping table on mysql -
i new mysql. have 2 tables.
table 1:product_details
manufacturerid| sku | image1 | image2 | image3 -------------------------------------------------------------------------------------------------- 123 | 1 | image1-sku1-filename.jpg | image2-sku1-filename.jpg | image3-sku1-filename.jpg 123 | 2 | image1-sku2-filename.jpg | image2-sku2-filename.jpg | image3-sku2-filename.jpg 123 | 3 | image1-sku3-filename.jpg | image2-sku3-filename.jpg | image3-sku3-filename.jpg 456 | 10 | image1-sku10-filename.jpg| image2-sku10-filename.jpg| image3-sku10-filename.jpg 456 | 20 | image1-sku20-filename.jpg| image2-sku20-filename.jpg| image3-sku20-filename.jpg
table 2:image_mapping
manufacturerid | image_column | image_type ------------------------------------------- 123 | image3 | master 123 | image1 | 123 | image2 | 456 | image3 | master
i need select query maps products (sku) product images (will replace image column names in table 2) per manufacturerid , per sku.
table 3: product_images
manufacturerid | sku | product_image | image_type -------------------------------------------------------------- 123 | 1 | image3-sku1-filename.jpg | master 123 | 1 | image1-sku1-filename.jpg | 123 | 1 | image2-sku1-filename.jpg | 123 | 2 | image3-sku2-filename.jpg | master 123 | 2 | image1-sku2-filename.jpg | 123 | 2 | image2-sku2-filename.jpg | 123 | 3 | image3-sku3-filename.jpg | master 123 | 3 | image1-sku3-filename.jpg | 123 | 3 | image2-sku3-filename.jpg | 456 | 10 | image3-sku10-filename.jpg| master 456 | 20 | image3-sku20-filename.jpg| master
the following works me data:
select pd.manufacturerid, sku, case when im.image_column = 'image1' pd.image1 when im.image_column = 'image2' pd.image2 when im.image_column = 'image3' pd.image3 end prod_image, im.image_type product_details pd join image_mapping im on pd.manufacturerid = im.manufacturerid;
sqlfiddle: http://sqlfiddle.com/#!9/76fc1/3/0
Comments
Post a Comment