2013-02-08 81 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 

通過這種方式,你可以鏈接每個有關係的表