2013-02-08 41 views
2

加入我的架构如下:http://acookson.org/wp-content/uploads/bookings.png查询使用链接表在MySQL

enter image description here

我试图用唯一的记录加入到拉。说'鲍勃'预订'2013-02-05 13:50:01' 与booking.id = 6。如何查询,加入并从课程表中搜索唯一记录?

我的表是填充了一些数据:

mysql> SELECT * from user; 
+----+--------+-----------------+ 
| id | name | email   | 
+----+--------+-----------------+ 
| 1 | bob | [email protected] | 
| 3 | sarah | [email protected] | 
| 4 | phil | [email protected] | 
| 5 | freda | [email protected] | 
| 6 | Sash | [email protected]  | 
| 7 | Glen | [email protected] | 
| 8 | Walter | [email protected] | 
+----+--------+-----------------+ 
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非常基本,所以正在寻找一些我可能如何真正查询这个模式的例子 。

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

回答

1

你很近 - 看看你的加入预订 - 使用user_id而不是id。它不应该是b.id = u.id(此连接你的用户ID你bookingid),而是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'; 

好运。

+0

感谢您指出,虽然使用上述查询,我​​仍然收到:空集(0.00秒) – cookie 2013-02-08 16:18:35

+0

如果我的“课程预订”表不接收独特的元组或基于我的查询的“课程预订”? – cookie 2013-02-09 07:06:46

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' 

为了充分获得知识约加盟,请访问以下链接:

+0

你上面的查询是不正确的 - 你加入预订两次......我已经回答了这个,虽然 - 为什么reanswer相同的答案:) – sgeddes 2013-02-08 15:10:57

0

你也可以像

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

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