使用MySQL中的连接查询链接表

时间:2013-02-08 15:02:55

标签: mysql sql join

我的架构如下:http://acookson.org/wp-content/uploads/bookings.png

enter image description here

我正在尝试使用连接来提取唯一记录。说'bob'在'2013-02-05 13:50:01'预订 预订.id = 6。如何从课程表中查询,加入和搜索唯一记录?

我的表格中填充了一些数据:

mysql> SELECT * from user; 
+----+--------+-----------------+
| id | name   | email           |
+----+--------+-----------------+
|  1 | bob    | bob@spam.com    |
|  3 | sarah  | sj@global.com   |
|  4 | phil   | pj2@arpanet.com |
|  5 | freda  | freda@gmail.com |
|  6 | Sash   | s@yahoo.com     |
|  7 | Glen   | glen@global.com |
|  8 | Walter | w@arpanet.com   |
+----+--------+-----------------+
7 rows in set (0.00 sec)

mysql> SELECT * from booking; 
+----+---------------------+---------+
| id | date                | user_id |
+----+---------------------+---------+
|  1 | 2013-02-08 12:28:24 |       1 |
|  4 | 2013-02-07 12:42:02 |       3 |
|  5 | 2013-02-05 12:42:46 |       4 |
|  6 | 2013-02-05 13:50:01 |       1 |
|  7 | 2013-02-01 13:50:01 |       3 |
|  8 | 2013-02-06 13:50:01 |       3 |
|  9 | 2013-01-29 13:50:01 |       4 |
+----+---------------------+---------+
7 rows in set (0.00 sec)

mysql> select * from lesson;
+----+-----------------------------+---------------------+---------------------+
| id | name                        | start_time          | end_time            |
+----+-----------------------------+---------------------+---------------------+
|  2 | CBT course                  | 2013-02-08 12:35:36 | 2013-02-08 13:35:36 |
|  3 | CBT course                  | 2013-02-15 11:59:44 | 2013-02-15 12:59:44 |
|  4 | Advanced Motorcyling module | 2013-02-15 12:04:29 | 2013-02-15 13:04:29 |
|  5 | CBT course                  | 2013-02-15 12:14:27 | 2013-02-15 13:14:27 |
|  6 | ABC course                  | 2013-02-13 13:28:13 | 2013-02-13 14:28:13 |
|  7 | LKU course                  | 2013-02-11 13:28:13 | 2013-02-11 14:28:13 |
|  8 | ERT starter course          | 2013-02-10 13:28:13 | 2013-02-10 14:28:13 |
+----+-----------------------------+---------------------+---------------------+
7 rows in set (0.00 sec)

我的

lesson_booking
表被定义为减少冗余,而这就是这个表 我正试图(间接)查询以返回结果。

我的查询如下:

SELECT * from user as u
JOIN booking AS b ON b.id = u.id
JOIN lesson_booking AS lb ON b.id = lb.booking_id 
JOIN lesson AS l ON lb.lesson_id = l.id 
WHERE u.name = 'bob';
Empty set (0.00 sec)

但这不会返回任何结果。我对MySQL非常基本,所以正在寻找一些例子 我如何真正地查询这个模式。

如果您可以向我提供几个(三个会做 - 不同的)示例,我将如何查询此数据集 那将是一种教育 - 我希望!

3 个答案:

答案 0 :(得分:2)

您当前查询的问题是您是从ID预订的ID用户加入错误的。应该是来自预订的ID的用户的user_ID

SELECT  a.*, b.*, c.*, d.*
FROM    booking a
        INNER JOIN user b
            ON a.user_ID = b.id
        INNER JOIN lesson_booking c
            ON a.id = c.booking_ID
        INNER JOIN lesson d
            ON c.lesson_ID = d.ID
WHERE   b.name = 'bob'

要充分了解联接知识,请访问以下链接:

答案 1 :(得分:1)

你很亲密 - 看看你的预订加入 - 使用user_id而不是id。它不应该是b.id = u.id(这会将你的用户ID加入你的预订),而是b.user_id = u.id:

SELECT * 
FROM user as u
   JOIN booking AS b ON b.user_id = u.id
   JOIN lesson_booking AS lb ON b.id = lb.booking_id 
   JOIN lesson AS l ON lb.lesson_id = l.id 
WHERE u.name = 'bob';
祝你好运。

答案 2 :(得分:0)

您还可以使用

select * from table1 a, table2 b 
where a.id = b.id

通过这种方式,您可以链接每个具有关系的表