我有两张桌子
studentdemo:
sid | date | status
--------------------------------
10 | 2013-12-28 | 1
11 | 2013-12-28 | 1
12 | 2013-12-28 | 1
13 | 2013-12-28 | 1
10 | 2013-12-30 | 1
11 | 2013-12-30 | 1
12 | 2013-12-30 | 1
13 | 2013-12-30 | 1
spdemo:
date | status
------------------------
2013-12-28 | cd
2013-12-29 | wd
2013-12-30 | cd
使用查询
SELECT *
FROM `studentdemo`
RIGHT JOIN spdemo
ON spdemo.date = studentdemo.date
WHERE spdemo.date BETWEEN "2013-12-28"
AND "2013-12-30"
导致日期2013-12-29
的空值:
NULL NULL NULL 2013-12-29 WD
是否可以使用sid获得输出?
sid | date | status | date | status
-------------------------------------------------------
10 | null | null | 2013-12-29 | wd
11 | null | null | 2013-12-29 | wd
12 | null | null | 2013-12-29 | wd
13 | null | null | 2013-12-29 | wd
答案 0 :(得分:0)
可以使用FULL OUTER JOIN解决。
但是目前MySQL不支持这种类型的JOIN。前段时间我写了a post关于MySQL中的FULL OUTER JOIN技巧。
答案 1 :(得分:0)
您可以交叉连接以获取所有组合,并LEFT JOIN
表格;
SELECT x.sid, table1.date, table1.status, x.date bdate, table2.status bstatus
FROM (SELECT DISTINCT table1.sid, table2.date
FROM table1 CROSS JOIN table2) x
LEFT JOIN table1 ON x.sid=table1.sid AND x.date = table1.date
LEFT JOIN table2 ON x.date=table2.date
ORDER BY bdate, sid