我正在尝试基于两个表创建一个视图。
成员:
ID Lang1_ID Lang2_ID - all tinyint
1 2 3
2 2 4
3 1 4
语言:
ID Title - tinyint, varchar(50)
1 English
2 French
3 Spanish
4 Portuguese
我希望创建一个包含所有标题的视图:
member_id, Lang1_Title, Lang2_Title
需要的结果:
1 French Spanish
2 French Portuguese
3 English Portuguese
答案 0 :(得分:1)
SELECT m.id member_id, l.Title Lang1_Title, l2.Title Lang2_Title
FROM Member m LEFT JOIN
Language l ON m.Lang1_ID = l.ID LEFT JOIN
Language l2 ON m.Lang2_ID = l2.ID
输出
| MEMBER_ID | LANG1_TITLE | LANG2_TITLE |
-----------------------------------------
| 1 | French | Spanish |
| 2 | French | Portuguese |
| 3 | English | Portuguese |
<强> sqlfiddle example 强>
视图
CREATE VIEW viewname
AS
SELECT m.id member_id, l.Title Lang1_Title, l2.Title Lang2_Title
FROM Member m LEFT JOIN
Language l ON m.Lang1_ID = l.ID LEFT JOIN
Language l2 ON m.Lang2_ID = l2.ID
并使用它
SELECT * FROM viewname
<强> sqlfiddle example 强>