我有以下mysql表和记录: http://sqlfiddle.com/#!9/fb7c68/1
我想创建一个将在3列中显示记录的查询
product_id, parent_option_id, parent_option_value_id (only if the parent_option_id > 0)
.
.
.
.
product_id, child_option_id, child_option_value_id (only if the child_option_id > 0)
.
.
.
.
product_id, grandchild_option_id, grandchild_option_value_id (only if the grandchild_option_id > 0)
答案 0 :(得分:1)
如果我正确地理解了你想做什么,可以尝试用UNION组合的3个查询。
SELECT
product_id as id,
parent_option_id as option_id,
parent_option_value_id as option_value_id
FROM option_value
WHERE
parent_option_id > 0
UNION
SELECT
product_id as id,
child_option_id as option_id,
child_option_value_id as option_value_id
FROM option_value
WHERE
child_option_id > 0
UNION
SELECT
product_id as id,
grandchild_option_id as option_id,
grandchild_option_value_id as option_value_id
FROM option_value
WHERE
grandchild_option_id > 0;