0

Suppose I have three table project, and I want to make a html with the correct values from database with php.

I have tried like this:

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

Evaluate table

-----------------------------------------------------------------------------
|     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 Table

-----------------------------------------------------------------------------
|     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 
-----------------------------------------------------------------------------

Here I want to query from three table like this and display the result.

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

1 Answers1

1

This is all the query you need:

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;

Everything else can and should be done in your application code.

Strawberry
  • 33,750
  • 13
  • 40
  • 57