按日期查询两表所有数据
table1(t1):
字段1 字段2 填写日期
1111 11 2011-1-1
2222 22 2011-2-1
3333 33 2011-3-1table2(t2):
字段1 字段2 填写日期
aaaa aa 2011-3-1
bbbb bb 2011-4-1我想查询出的结果是:t1.字段1 t1.字段2 填写日期 t2.字段1 t2.字段2
1111 11 2011-1-1 null null
2222 22 2011-2-1 null null
3333 33 2011-3-1 aaaa aa
null null 2011-4-1 bbbb bb这样要怎么写SQL语句才能查到?
table1(t1):
字段1 字段2 填写日期
1111 11 2011-1-1
2222 22 2011-2-1
3333 33 2011-3-1table2(t2):
字段1 字段2 填写日期
aaaa aa 2011-3-1
bbbb bb 2011-4-1我想查询出的结果是:t1.字段1 t1.字段2 填写日期 t2.字段1 t2.字段2
1111 11 2011-1-1 null null
2222 22 2011-2-1 null null
3333 33 2011-3-1 aaaa aa
null null 2011-4-1 bbbb bb这样要怎么写SQL语句才能查到?
select t1.字段,t1.字段2,填写日期,t2.字段1,t2.字段2 from t1 left join t2 on t1.填写日期=t2.填写日期 union
select t1.字段,t1.字段2,填写日期,t2.字段1,t2.字段2 from t1 right join t2 on t1.填写日期=t2.填写日期
user表:
U_ID Name
查出结果如:
Name t1.字段1 t1.字段2 填写日期 t2.字段1 t2.字段2
c213135 1111 11 2011-1-1 null null
michael 2222 22 2011-2-1 null null
WWWWA 3333 33 2011-3-1 aaaa aa
rucypli null null 2011-4-1 bbbb bb
From table1 left join table2 on table1.填写日期= table2.填写日期
Union all
Select*
From table1 right join table2 on table1.填写日期= table2.填写日期 where table1.填写日期 is null