加入我的架構如下:http://acookson.org/wp-content/uploads/bookings.png查詢使用鏈接表在MySQL
我試圖用唯一的記錄加入到拉。說'鮑勃'預訂'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非常基本,所以正在尋找一些我可能如何真正查詢這個模式的例子 。
如果你能爲我提供幾個例子(三個會做不同的)我如何查詢這個數據集 那麼這將是一個教育 - 我希望!
感謝您指出,雖然使用上述查詢,我仍然收到:空集(0.00秒) – cookie 2013-02-08 16:18:35
如果我的「課程預訂」表不接收獨特的元組或基於我的查詢的「課程預訂」? – cookie 2013-02-09 07:06:46