使用INNER JOIN(S)這樣的期望輸出
SELECT f.id,
o6.option_name as element_6,
o7.option_name as element_7,
o8.option_name as element_8,
o9.option_name as element_9
FROM form_123 f
INNER JOIN options o6 ON f.element_6 = o6.option_id
INNER JOIN options o7 ON f.element_7 = o7.option_id
INNER JOIN options o8 ON f.element_8 = o8.option_id
INNER JOIN options o9 ON f.element_9 = o9.option_id
ORDER BY f.id ASC
sqlfiddle 您的樣本順便說一句,似乎關閉。
輸出
id element_6 element_7 element_8 element_9
1 sausages buckfast grapes buckfast
2 fish foo bananas fish
3 grapes grapes bread bananas
如果你有更多formid(S)即將到來,你可以添加所有formids 123 這樣
SELECT f.id,
o6.option_name as element_6,
o7.option_name as element_7,
o8.option_name as element_8,
o9.option_name as element_9
FROM form_123 f
INNER JOIN options o6 ON f.element_6 = o6.option_id
INNER JOIN options o7 ON f.element_7 = o7.option_id
INNER JOIN options o8 ON f.element_8 = o8.option_id
INNER JOIN options o9 ON f.element_9 = o9.option_id
WHERE o6.formid = 123
AND o7.formid = 123
AND o8.formid = 123
AND o9.formid = 123
ORDER BY f.id ASC
,或者你可以只添加和formid = 123這樣的開啓條件下的條件
SELECT f.id,
o6.option_name as element_6,
o7.option_name as element_7,
o8.option_name as element_8,
o9.option_name as element_9
FROM form_123 f
INNER JOIN options o6 ON f.element_6 = o6.option_id AND o6.formid = 123
INNER JOIN options o7 ON f.element_7 = o7.option_id AND o7.formid = 123
INNER JOIN options o8 ON f.element_8 = o8.option_id AND o8.formid = 123
INNER JOIN options o9 ON f.element_9 = o9.option_id AND o9.formid = 123
ORDER BY f.id ASC
編輯以糾正預期的輸出 – brianilland