我有三個表,我希望從表中選擇而不產生重複。Mysql - 從多個表中選擇而不產生重複的數據
表如下:
客戶
id | name | lastName
---------------------------------------
1 | john | doe
2 | helen | keller
訂單
的用戶ID列是一個外鍵引用李四,所以約翰訂單3項。
id | userID | order
---------------------------------------
1 | 1 | pizza
2 | 1 | pasta
3 | 1 | lasagna
CustomerRating
的用戶ID列是一個外鍵引用李四,所以約翰·葉5條的評論。
id | userID | rating | comment
-------------------------------------------------
1 | 1 | 5/5 | was good
2 | 1 | 5/5 | excellent
3 | 1 | 4/5 | great
4 | 1 | 4/5 | great
5 | 1 | 4/5 | great
我該如何從3個表格中選擇哪裏可以得到像這樣的返回結果?
id | name | lastName | order | rating
-----------------------------------------------------------------
1 | john | doe | pasta | 5/5
| | | pizza | 5/5
| | | lasagna | 4/5
| | | | 4/5
| | | | 4/5
我已經試過加入這些表,但由於約翰已經離開5條的評論,只下令3次,ID,姓名,姓氏和訂單列被填充了重複數據。
謝謝!
可能重複的最後一個(http://stackoverflow.com/questions/6066197/selecting -across-multiple-tables-with-union) – worldofjr 2014-12-07 08:03:53
發佈你想要的結果 – Yang 2014-12-07 08:19:34
@worldofjr聯合查詢需要具有相同的列數,所以對我來說不起作用。 – hmzfier 2014-12-07 08:58:47