开发者

Mysql Query JOIN Query

I h开发者_JS百科ave three tables. Table A has the option name (i.e. Color, Size). Table B has option value name (i.e Blue, Red, Black, Etc.). Table C establishes the relationship by putting option name id and option name value id together in a row.

My query needs to show the names of the values and and options as opposed to the id number. I can do A and B one at a time and get a list of value and options names, but since the value and options are related I want a combined list. For Example.

Color - Blue Color - Red Size - Big Size - Small

So my question is how to join these two queries to get a combined result like above.

SELECT products_options.products_options_name FROM products_options
LEFT JOIN products_options_values_to_products_options ON products_options_values_to_products_options.products_options_id=products_options.products_options_id

SELECT products_options_values.products_options_values_name FROM products_options_values
LEFT JOIN products_options_values_to_products_options ON products_options_values_to_products_options.products_options_values_id=products_options_values.products_options_values_id


You need two joins, which will look like this:

select product_options.product_id,
       option_names.name_id,
       option_names.name,
       option_values.value_id,
       option_values.name
from product_options
join option_names using (option_id)
join option_values using (option_id)
where product_options.product_id = :product_id

It won't combine them together for an immediate insert in your variations table, but this is the best you'll do in MySQL.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜