2017-05-30 76 views
2

我有以下表MySQL查詢顯示成分

mysql> select * from drink; 
+----+--------------+----------+---------+ 
| id | name   | location | alcohol | 
+----+--------------+----------+---------+ 
| 1 | Ginger Ale |  13 |  0 | 
| 2 | Whiskey  |  1 |  1 | 
| 3 | Vodka  |  2 |  1 | 
| 4 | Rum   |  3 |  1 | 
| 5 | Gin   |  4 |  1 | 
| 6 | Tequila  |  5 |  1 | 
| 7 | Triple Sec |  6 |  1 | 
| 8 | Cola   |  14 |  0 | 
| 10 | Cherry Vodka |  7 |  1 | 
| 11 | Sprite  |  15 |  0 | 
| 12 | Sour Mix  |  0 |  0 | 
| 13 | Lemon Juice |  0 |  0 | 
| 14 | Lime Juice |  0 |  0 | 
| 15 | Grenadine |  0 |  0 | 
| 16 | Orange Juice |  16 |  0 | 
+----+--------------+----------+---------+ 
15 rows in set (0.00 sec) 

mysql> select * from cocktail; 
+----+-------+----------------------------------+ 
| id | name | comment       | 
+----+-------+----------------------------------+ 
| 1 | Item1 | Whiskey Ginger     | 
| 2 | Item2 | Screwdriver      | 
| 3 | Item3 | Cherry Vodka and Sprite   | 
| 4 | Item4 | Bartender's Long Island Iced Tea | 
| 5 | Item5 | Long Island Iced Tea    | 
| 6 | Item6 | Gin and Sin      | 
+----+-------+----------------------------------+ 
6 rows in set (0.00 sec) 

mysql> select * from mix; 
+----+---------+------------+--------+ 
| id | drinkID | cocktailID | ounces | 
+----+---------+------------+--------+ 
| 1 |  1 |   1 | 4.00 | 
| 2 |  2 |   1 | 1.00 | 
| 9 |  10 |   3 | 1.00 | 
| 10 |  11 |   3 | 4.00 | 
| 11 |  5 |   4 | 0.75 | 
| 12 |  4 |   4 | 0.75 | 
| 13 |  6 |   4 | 0.75 | 
| 14 |  3 |   4 | 0.75 | 
| 15 |  7 |   4 | 1.00 | 
| 16 |  12 |   4 | 1.00 | 
| 17 |  8 |   4 | 1.00 | 
| 18 |  3 |   5 | 0.75 | 
| 19 |  5 |   5 | 0.75 | 
| 20 |  4 |   5 | 0.75 | 
| 21 |  6 |   5 | 0.75 | 
| 22 |  12 |   5 | 2.00 | 
| 23 |  8 |   5 | 1.00 | 
| 24 |  7 |   5 | 1.00 | 
| 25 |  5 |   6 | 2.00 | 
| 26 |  13 |   6 | 2.00 | 
| 27 |  15 |   6 | 2.00 | 
| 28 |  16 |   6 | 2.00 | 
| 33 |  3 |   2 | 1.50 | 
| 34 |  16 |   2 | 4.00 | 
+----+---------+------------+--------+ 
24 rows in set (0.00 sec) 

從這些表我想創建一個具有ID,評論,成分名稱,並盎司數的連接表。

到目前爲止,我有這個查詢,但我不斷收到錯誤,我不知道哪裏出錯了。

select cocktail.comment 
     , drink.name 
     , mix.ounces 
    from cocktail 
    join mix 
    on mix.drinkID = drink.ID 
    join cocktail 
    on mix.cocktailID = cocktail.id; 

我認爲這有什麼不對我加入語句,但是在MySQL錯誤是不明確的,以我

ERROR 1066 (42000): Not unique table/alias: 'cocktail' 

會有人能夠給我提供一些指導?謝謝!

+0

你能提供表格結構嗎? –

+0

請注意,mix table上的代理id沒有實際用途。 – Strawberry

回答

1

您正在查詢的是撥打cocktail表兩次。如果你想這樣做,你需要使用一個別名給呼叫提供唯一的名稱,但看起來你缺少JOINdrink表。看起來像你想要的:

select 
    c.comment, 
    d.name, 
    m.ounces 
from drink d 
inner join mix m 
    on m.drinkID = d.ID 
inner join cocktail c 
    on m.cocktailID = c.id; 

這是一個demo。這個版本和你的版本不同之處在於它包含一個致電drink表格以獲取配料的名稱。