56,678
社区成员
发帖
与我相关
我的任务
分享
mysql> SELECT a.id,a.a.record_date,
-> IFNULL(num,0)
-> FROM (
-> SELECT DISTINCT id,record_date FROM tb2a,tb1a) a LEFT JOIN tb2a b ON a.
=b.`tb1_id` AND a.record_date=b.`record_date`;
+------+-------------+---------------+
| id | record_date | IFNULL(num,0) |
+------+-------------+---------------+
| 1 | 2011-08-15 | 10 |
| 2 | 2011-08-15 | 11 |
| 3 | 2011-08-15 | 0 |
| 1 | 2011-08-16 | 12 |
| 2 | 2011-08-16 | 13 |
| 3 | 2011-08-16 | 14 |
| 1 | 2011-08-17 | 0 |
| 2 | 2011-08-17 | 15 |
| 3 | 2011-08-17 | 0 |
+------+-------------+---------------+
9 rows in set (0.00 sec)
mysql>
drop table if exists tempRecordDate;
create temporary table tempRecordDate(
record_date char(10)
);
insert into tempRecordDate
select distinct record_date from tb2;
select id, ifnull(num, 0) as num, tmp.record_date
from tempRecordDate as tmp join tb1 left join tb2
on id = tb1_id and tmp.record_date = tb2.record_date
order by tmp.record_date, id