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

时间:2015-12-08 10:27:59

标签: mysql sql multiple-tables

我希望我的表格输出类似这样的内容

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

以下是我的数据库

上的表格

会议表

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

用户表

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

位置表

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

时间表

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

日期表

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

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

我知道我提出的查询费用很高,但我不知道如何进行更优化的查询。我甚至不知道是否可以将结果仅用于单个查询。任何帮助都很好。感谢。

1 个答案:

答案 0 :(得分:1)

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

小心,当你在SQL上使用列名时,语法是TABLE.COLUMN_NAME,看起来你经常错误地退出订单...

我更改了一些表格名称,因为有时您最后会使用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 |