mysql内连接、左连接、右连接及union查询
内连接查询
内连接是把2张表符合条件的行左右拼接在一起,select xxx from table1 inner join table2 on table1.xxx = table2.xxx;比如,select * from boy inner join girl on boy.hid = girl.hid;

左链接查询
左连接是是以左边的数据为准,查询右边的相关数据,查不到的补NULL,比如,select * from boy left join girl on boy.hid = girl.hid;

右链接查询
右连接是是以右边的数据为准,查询左边的相关数据,查不到的补NULL,比如,select * from boy right join girl on boy.hid = girl.hid;

union查询
union查询是指把2条或多条sql的查询结果,上下合并成一个结果集,union必须满足一个条件:各语句查询的结果的列必须相同,列名称不需要相同,合并的结果以第一条sql的列名称为准,注意,union子句不用order by,使用union时相同的行会被合并,合并比较耗时,一般不使用,不想合并可以使用union all,比如,select * from a union all select * from b;


浙公网安备 33010602011771号