3

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

在此处输入图像描述

我正在尝试使用连接来提取唯一记录。假设 'bob' 在 '2013-02-05 13:50:01' 进行了预订,booking.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)

我的

课程预订
表的定义是为了减少冗余,我试图(间接地)查询这个表以返回结果。

我的查询看起来像:

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 非常了解,因此正在寻找一些关于如何真正查询此模式的示例。

如果你能给我提供几个(三个会做 - 不同的)我如何查询这个数据集的例子,那么这将是一种教育 - 我希望!

4

3 回答 3

4

您当前查询的问题是您正在加入来自预订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'

要全面了解联接,请访问以下链接:

于 2013-02-08T15:05:58.857 回答
3

你很接近——看看你在预订时的加入——使用 user_id 而不是 id。它不应该是 b.id = u.id(这会将您的用户 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';

祝你好运。

于 2013-02-08T15:04:26.860 回答
0

你也可以像

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

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

于 2013-02-08T15:12:51.993 回答