table1(t1):
字段1 字段2 填写日期
1111 11 2011-1-1
2222 22 2011-2-1
3333 33 2011-3-1
table2(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语句才能查到?
5 个解决方案
#1
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.填写日期
#2
mysql没有FULL JOIN,LEFT JOIN+RIGHT JOIN
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.填写日期
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.填写日期
#3
left join +right join + union
#4
那么如果我的两个表都有一个U_ID的字段,引用自令一个表,要加点什么可以将用户野查出来?
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
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
#5
Select*
From table1 left join table2 on table1.填写日期= table2.填写日期
Union all
Select*
From table1 right join table2 on table1.填写日期= table2.填写日期 where table1.填写日期 is null
From table1 left join table2 on table1.填写日期= table2.填写日期
Union all
Select*
From table1 right join table2 on table1.填写日期= table2.填写日期 where table1.填写日期 is null
#1
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.填写日期
#2
mysql没有FULL JOIN,LEFT JOIN+RIGHT JOIN
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.填写日期
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.填写日期
#3
left join +right join + union
#4
那么如果我的两个表都有一个U_ID的字段,引用自令一个表,要加点什么可以将用户野查出来?
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
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
#5
Select*
From table1 left join table2 on table1.填写日期= table2.填写日期
Union all
Select*
From table1 right join table2 on table1.填写日期= table2.填写日期 where table1.填写日期 is null
From table1 left join table2 on table1.填写日期= table2.填写日期
Union all
Select*
From table1 right join table2 on table1.填写日期= table2.填写日期 where table1.填写日期 is null