如何从3个表中进行查询

时间:2019-03-25 16:07:46

标签: php mysql

假设我有三个表项目,并且我想用php从数据库中提取一个具有正确值的html。

我尝试过这样:

select (SELECT group_concat(DISTINCT wine_name) FROM wine) as wine_name UNION SELECT username FROM user;

评估表格

-----------------------------------------------------------------------------
|     ev_id     |     user_id       |     wine_id       |        point
-----------------------------------------------------------------------------
|       1       |         1         |       1           |         80
|       2       |         2         |       1           |         67
|       3       |         1         |       2           |         87
|       5       |         2         |       2           |         97
|       6       |         2         |       3           |         81
|       7       |         3         |       2           |         99
|       8       |         3         |       3           |         66
|       9       |         3         |       1           |         79
-----------------------------------------------------------------------------

酒桌

-----------------------------------------------------------------------------
|     wine_id   |     wine_name     |     wine_type     |     wine_color
-----------------------------------------------------------------------------
|       1       |   Dominio Campo   |        sweet      |         red
|       2       |   Guitian Godello |        dry        |         white
|       3       |   Cosme Palacio   |        dry        |         red
|       4       |   Azpilicueta     |        sweet      |         red
|       5       |   Parotet Vermell |        sweet      |         white
-----------------------------------------------------------------------------

User Table
-----------------------------------------------------------------------------
|     user_id   |     user name     |        name       |       email
-----------------------------------------------------------------------------
|       1       |     user1         |        dsa        |     asd@asd.com
|       2       |     user2         |        dsd        |     abd@asd.com 
|       3       |     user3         |        dss        |     acd@asd.com
|       4       |     user4         |        sdd        |     add@asd.com
|       5       |     user5         |        ssd        |     aed@asd.com 
-----------------------------------------------------------------------------

在这里我要从三个表中查询并显示结果。

-----------------------------------------------------------------------------
|                |   Dominio Campo   |   Guitian Godello |   Cosme Palacio   |
-----------------------------------------------------------------------------
|    user1       |       80          |       87          |       77          |
|    user2       |       67          |       97          |       81          |
|    user3       |       79          |       99          |       66          |
-----------------------------------------------------------------------------

1 个答案:

答案 0 :(得分:1)

这就是您需要的所有查询:

SELECT w.*
     , e.user_id
     , e.point 
  FROM wine w 
  JOIN evaluate e 
    ON e.wine_id = w.wine_id 
 ORDER 
    BY e.wine_id
     , user_id;

其他所有事情都可以并且应该在您的应用程序代码中完成。