JOIN语句基础
join test1
#join test1 查询t1、t2表的交集部分
select * from t_test t1
inner join t_test2 t2 on t1.id=t2.id;
join test2
#join test2
select * from t_test t1
right join t_test2 t2 on t1.id=t2.id;
join test3
#join test3
select * from t_test t1 left join t_test2 t2 on t1.id=t2.id;
join test4
#join test4
select * from t_test t1
left join t_test2 t2 on t1.id=t2.id
where t2.id is null;
join test5
#join test5
select * from t_test t1
right join t_test2 t2 on t1.id=t2.id
where t1.id is null;
join test6
#join test6
select * from t_test t1
left join t_test2 t2 on t1.id=t2.id
union
select * from t_test t1
right join t_test2 t2 on t1.id=t2.id;
join test7
#join test7
select * from t_test t1
left join t_test2 t2 on t1.id=t2.id
where t2.id is null
union
select * from t_test t1
right join t_test2 t2 on t1.id=t2.id
where t1.id is null;
SQL执行顺序
手写的SQL
select <distinct > <select content>
from <table name>
<join type> on < join condition>
where
<where condition>
group by <group by condition>
having <having condition>
order by <order by condition>
limit <limit condition>
手写代码经过MySQL解析器解析成机器识别的SQL
from <table name>
<join type> on < join condition>
where
<where condition>
group by <group by condition>
having <having condition>
select <distinct > <select content>
order by <order by condition>
limit <limit condition>
SQL鱼骨图













网友评论