MySQL Query显示成分

时间:2017-05-30 17:01:23

标签: 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;

我认为我的join语句有问题,但MySQL错误对我来说是不明确的

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

有人能为我提供一些指导吗?谢谢!

1 个答案:

答案 0 :(得分: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表的调用以获取该成分的名称。