我有两个表,我想从attribute_option表中选择所有行,并根据option_id
表的product_attribute
列对它们进行排序:
attribute_option
option_id | attribute_id | option_name
4 | 2 | f1
5 | 2 | f2
9 | 2 | f3
10 | 2 | f4
17 | 2 | f5
18 | 2 | f6
24 | 2 | f7
product_attribute
rec_id | product_id | attibute_id | option_id
1 | 1 | 2 | 4
2 | 1 | 2 | 5
3 | 5 | 2 | 25
4 | 5 | 2 | 4
对于产品ID 5 attribute_option 表应该是这样的
option_id | attribute_id | option_name
25 | 2 | f8
4 | 2 | f1
5 | 2 | f2
9 | 2 | f3
10 | 2 | f4
17 | 2 | f5
18 | 2 | f6
24 | 2 | f7
答案 0 :(得分:-1)
SELECT attribute_option.option_id,
attribute_option.attribute_id,
attribute_option.option_name
FROM attribute_option,
product_attribute
WHERE product_attribute.option_id = attribute_option.option_id
ORDER BY product_attribute.option_id
我认为这就是你要求的......
答案 1 :(得分:-1)
请检查一下。它完美地运作。准确提供您需要的输出。 http://sqlfiddle.com/#!2/e952e/4/0
SELECT attribute_option.option_id,
attribute_option.attribute_id,
attribute_option.option_name
FROM attribute_option,
product_attribute
WHERE product_attribute.option_id = attribute_option.option_id
AND product_attribute.product_id = 5
UNION
SELECT attribute_option.option_id,
attribute_option.attribute_id,
attribute_option.option_name
FROM attribute_option,
product_attribute
WHERE product_attribute.product_id != 5