我有两张桌子。
表 db1 和 db2
DB1
+-----------+------------+
| id | nameDb |
+-----------+------------+
| 1 | name1 |
+-----------+------------+
| 2 | name2 |
+-----------+------------+
| 3 | name3 |
+-----------+------------+
DB2
+------------+------------+-------------+----------------------+
| id | name | id_db1 | date |
+------------+------------+-------------+----------------------+
| 1 | test1 | 1 | 2013-05-10 10:00:00 |
+------------+------------+-------------+----------------------+
| 2 | test2 | 1 | 2013-05-10 11:00:00 |
+------------+------------+-------------+----------------------+
| 3 | test3 | 1 | 2013-05-10 11:10:00 |
+------------+------------+-------------+----------------------+
| 4 | test4 | 1 | 2013-05-10 11:40:00 |
+------------+------------+-------------+----------------------+
我的查询是这样的:
SELECT a.nameDb, b.name FROM db1 a
LEFT JOIN db2 b ON b.id_db1 = a.id
WHERE DATE_FORMAT(b.name, '%Y-%m-%d') = '2013-05-10'
我需要返回与当天相关的所有内容,不相关的也必须出现......
就像是
+------------+------------+
| nameDb | name |
+------------+------------+
| name1 | test1 |
+------------+------------+
| name1 | test2 |
+------------+------------+
| name1 | test3 |
+------------+------------+
| name1 | test4 |
+------------+------------+
| name2 | NULL |
+------------+------------+
| name3 | NULL |
+------------+------------+
任何想法 ?