如何使用单个查询查询多个表?

How to query multiple tables using a single query?

我希望我的 table 输出这样的东西

---------------------------------------------------------------------------------------------
|  date         |  location | time  |        delegate 1         |       delegate 2          |
|--------------------------------------------------------------------------------------------
|  2015-12-07   |  Table 1  | 9:00  |  first_name_4 last_name_4 | first_name_5 last_name_5  |
|--------------------------------------------------------------------------------------------
|                           | 9:30  |  first_name_4 last_name_4 | first_name_6 last_name_6  |
|--------------------------------------------------------------------------------------------
|                           | 9:30  |  first_name_3 last_name_3 | first_name_7 last_name_7  |
|--------------------------------------------------------------------------------------------
|                           | 9:00  |  first_name_3 last_name_3 | first_name_7 last_name_7  |
|--------------------------------------------------------------------------------------------

这是我数据库中的 table

会议table

-------------------------------------------------------------------------------------------------
|  id   |  date_id  | time_id   | location_id   | delegate_id_1 |   delegate_id_2   | status    |
|------------------------------------------------------------------------------------------------
|   1   |     1     |     1     |       1       |       4       |         5         |     A     |
|------------------------------------------------------------------------------------------------
|   2   |     1     |     2     |       1       |       4       |         6         |     A     |
|------------------------------------------------------------------------------------------------
|   3   |     1     |     1     |       1       |       2       |         6         |     P     |
|------------------------------------------------------------------------------------------------
|   4   |     1     |     2     |       1       |       1       |         3         |     A     |
|------------------------------------------------------------------------------------------------
|   5   |     1     |     1     |       1       |       1       |         3         |     A     |
|------------------------------------------------------------------------------------------------

用户table

-----------------------------------------
|  id   |  first_name   | last_name     | 
|----------------------------------------
|  1    |  first_name_1 | last_name_1   | 
|----------------------------------------
|  2    |  first_name_2 | last_name_2   | 
|----------------------------------------
|  3    |  first_name_3 | last_name_3   | 
|----------------------------------------
|  4    |  first_name_4 | last_name_4   | 
|----------------------------------------
|  5    |  first_name_5 | last_name_5   | 
|----------------------------------------
|  6    |  first_name_6 | last_name_6   | 
|----------------------------------------

地点table

-----------------------------
|  id   |  location_name    | 
|----------------------------
|  1    |  Table 1          | 
|----------------------------

时间table

-------------------------
|  id   |  meeting_time | 
|------------------------
|  1    |  9:00:00      | 
|------------------------
|  1    |  9:30:00      | 
|------------------------

日期table

-------------------------
|  id   |  meeting_date | 
|------------------------
|  1    |  2015-12-07   | 
|------------------------
|  2    |  2015-12-08   | 
|------------------------
|  3    |  2015-12-09   | 
|------------------------

我的初始查询是这样的

-- $query_date
SELECT meeting_date 
FROM dates 
WHERE meeting_date = '2015-12-07'

-- $query_location
SELECT location_name.location 
from location 
LEFT JOIN meetings 
    ON meetings.location_id=location.id 
LEFT JOIN date 
    ON meetings.date_id=date.id 
WHERE meeting_date.dates = '2015-12-07'

现在,这是我弄错的部分。

-- $query_final
SELECT meeting_time.time, delegate1.first_name AS first_name_1,
   delegate1.last_name AS last_name_1, delegate2.first_name AS first_name_2,
   delegate2.last_name AS last_name_2 
FROM meetings 
INNER JOIN users delegate1 
    ON meetings.delegate_id_1=users.id 
LEFT JOIN users delegate2 
    ON meetings.delegate_id_2=users.id 
WHERE meetings.status='A'

我上次查询的结果给了我意想不到的结果,因为结果显示的条目比我的会议多 table。

我知道我所做的查询成本很高,但我不知道如何进行更优化的查询。我什至不知道是否有可能只将结果放入单个查询中。任何帮助都很好。谢谢。

您可以通过使用正确的 JOIN 的单个查询来恢复所有内容。

小心,当你在 SQL 上使用列名时,语法是 TABLE.COLUMN_NAME,看来你经常在命令 quit 上出错...

我更改了一些 table 名称,因为您有时在末尾使用 s 而有时没有。

由于timedate是SQL关键字,最好用s到处

SQL Fiddle

MySQL 5.6 架构设置:

CREATE TABLE meetings  (`id` int, `date_id` int, `time_id` int, `location_id` int, `delegate_id_1` int, `delegate_id_2` int, `status` varchar(1));
INSERT INTO meetings (`id`, `date_id`, `time_id`, `location_id`, `delegate_id_1`, `delegate_id_2`, `status`)
VALUES (1, 1, 1, 1, 4, 5, 'A'),
    (2, 1, 2, 1, 4, 6, 'A'),
    (3, 1, 1, 1, 2, 6, 'P'),
    (4, 1, 2, 1, 1, 3, 'A'),
    (5, 1, 1, 1, 1, 3, 'A');

CREATE TABLE users (`id` int, `first_name` varchar(12), `last_name` varchar(11));
INSERT INTO users (`id`, `first_name`, `last_name`)
VALUES (1, 'first_name_1', 'last_name_1'),
    (2, 'first_name_2', 'last_name_2'),
    (3, 'first_name_3', 'last_name_3'),
    (4, 'first_name_4', 'last_name_4'),
    (5, 'first_name_5', 'last_name_5'),
    (6, 'first_name_6', 'last_name_6');

CREATE TABLE locations (`id` int, `location_name` varchar(7));
INSERT INTO locations (`id`, `location_name`)
VALUES (1, 'Table 1');

CREATE TABLE times (`id` int, `meeting_time` varchar(7));
INSERT INTO times (`id`, `meeting_time`)
VALUES  (1, '9:00:00'),
    (2, '9:30:00') ;

CREATE TABLE dates (`id` int, `meeting_date` varchar(10)) ;
INSERT INTO dates (`id`, `meeting_date`)
VALUES (1, '2015-12-07'),
    (2, '2015-12-08'),
    (3, '2015-12-09') ;

查询 1:

-- $query_final
SELECT locations.location_name,
   `times`.meeting_time, 
   delegate1.first_name AS first_name_1,
   delegate1.last_name AS last_name_1, 
   delegate2.first_name AS first_name_2,
   delegate2.last_name AS last_name_2 
FROM meetings 

LEFT JOIN locations 
    ON meetings.location_id=locations.id 
LEFT JOIN dates 
    ON meetings.date_id=`dates`.id 
LEFT JOIN times 
    ON meetings.time_id=`times`.id 
INNER JOIN users delegate1 
    ON meetings.delegate_id_1 = delegate1.id 
LEFT JOIN users delegate2 
    ON meetings.delegate_id_2 = delegate2.id 

WHERE 
    meetings.status = 'A'
    AND dates.meeting_date = '2015-12-07'

Results:

| location_name | meeting_time |   first_name |   last_name |   first_name |   last_name |
|---------------|--------------|--------------|-------------|--------------|-------------|
|       Table 1 |      9:00:00 | first_name_1 | last_name_1 | first_name_3 | last_name_3 |
|       Table 1 |      9:30:00 | first_name_1 | last_name_1 | first_name_3 | last_name_3 |
|       Table 1 |      9:00:00 | first_name_4 | last_name_4 | first_name_5 | last_name_5 |
|       Table 1 |      9:30:00 | first_name_4 | last_name_4 | first_name_6 | last_name_6 |