有以下兩張表mysql
a +------+------+---------+ | uid | name | addtime | +------+------+---------+ | 1 | tom | 2001 | | 2 | jack | 2002 | | 3 | alex | 2003 | +------+------+---------+ b +------+-------+---------+ | uid | title | addtime | +------+-------+---------+ | 1 | rain | 2004 | | 2 | snow | 2005 | | 3 | wind | 2006 | +------+-------+---------+
join是將兩個表橫向合併,就是經過on的條件將一個表追加在另外一個表後邊,新表相對於原來的兩個表字段增多了。sql
mysql> select * from b left join a on b.uid = a.uid; +------+-------+---------+------+------+---------+ | uid | title | addtime | uid | name | addtime | +------+-------+---------+------+------+---------+ | 1 | rain | 2004 | 1 | tom | 2001 | | 2 | snow | 2005 | 2 | jack | 2002 | | 3 | wind | 2006 | 3 | alex | 2003 | +------+-------+---------+------+------+---------+
union是將兩個表豎向合併,新表字段不會多於原表。ui
mysql> select * from b union select * from a; +------+-------+---------+ | uid | title | addtime | +------+-------+---------+ | 1 | rain | 2004 | | 2 | snow | 2005 | | 3 | wind | 2006 | | 1 | tom | 2001 | | 2 | jack | 2002 | | 3 | alex | 2003 | +------+-------+---------+ mysql> select title,addtime from b union select name,addtime from a; +-------+---------+ | title | addtime | +-------+---------+ | rain | 2004 | | snow | 2005 | | wind | 2006 | | tom | 2001 | | jack | 2002 | | alex | 2003 | +-------+---------+
參考:http://bbs.csdn.net/topics/390410010spa