我有一个 sql 语句的问题。 我的数据库是 MySQL 5.1。
我有 2 个表:
day_table
date(pk) note ...
------- ----- ---
2012-03-29 foo ...
2012-03-30 bar ...
2012-03-31 foobar ...
other_table
id fk_date key value
-- ------- --- -----
1 2012-03-29 foo 5
2 2012-03-30 bar 9
3 2012-03-30 foo 4
4 2012-03-31 bar 6
5 2012-03-31 foo 1
所以我需要这样的查询:
SELECT o.value, d.date FROM other_table o join day_table d on (...) where key = "bar" group by d.date;
我需要这样的结果:
result_table
date(pk) value
------- -----
2012-03-29 0 (or null) <---- THIS IS IMPORTANT!!!
2012-03-30 9
2012-03-31 6
我知道问题出在连接上,但我需要获取 day_table 中的每个日期。
(My)SQL 可以吗?
最好的问候
最佳答案
使用左连接而不是内连接:
SELECT o.value, d.date FROM day_table o left join other_table d on o.date=d.date and key = "bar" group by d.date;
关于mysql - SQL返回属性不存在的连接的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9972914/