MySQL的执行顺序和Join

sql执行顺序

select

distinct <select_list> 

form table_left

<inner join><left join><rigth join> table_right  on  <join_condition>

where <where_condition>

group by <group_by_list>

having <having_condition>

order by <order_by_list>

limit <limit_number>

上面的执行过程

1.from table_left
2.on  <join_condition>
3.<inner join><left join><rigth join> table_right
4.where <where_condition>
5.group by <group_by_list>
6.having <having_condition>
7.select
8.distinct <select_list> 
9.order by <order_by_list>
10.limit <limit_number>

Join的七种理论

 MySQL的执行顺序和Join

 

--MySQL Full Join的实现 因为MySQL不支持FULL JOIN,下面是替代方法.
SELECT * FROM TABLEA A LEFT JOIN TABLEB B ON A.Key= B.Key
UNION
SELECT * FROM TABLEA A RIGHT JOIN TABLEB B ON A.Key= B.key
--这里因为要联合的缘故,不能考虑到小表驱动大表的情况。只能用right join。要保证查询出来的数字要一致。A的独有+B的独有
SELECT * FROM TABLEA A LEFT JOIN TABLEB B ON A.Key = B.Key WHERE B.Key IS NULL
UNION
SELECT * FROM TABLEA A RIGHT JOIN TABLEB B ON A.Key= B.Key WHERE A.Key IS NULL;

 

MySQL的执行顺序和Join

上一篇:sqlServer学习1-sql脚本


下一篇:mysql学习--sql基本介绍