mysql之union
MySQL UNION 操作符用于连接两个以上的 SELECT 语句的结果组合到一个结果集合中。多个 SELECT 语句会删除重复的数据。
特性1:两个表的数据会展示成1列,列名为第一个查询语句的被查询列为主
select title from testgroup.m_blog union
select username from testgroup.m_user ;
特性2:第一个查询语句和第二个查询语句,查询的列数要一致才可以
演示:查询多列
select id, user_id, title, description, content from testgroup.m_blog union
select id, username, avatar, email ,password from testgroup.m_user ;
ALL
为了演示all,我们修改一下数据库,我们把m_user的id为1的username修改为“文章一”,这样就可以和m_blog的id为1且title为“文章一”的记录保持一致。
select id, title from testgroup.m_blog union all
select id, username from testgroup.m_user order by id;
我们再来试一下不加all的情况,发现不加all的被去重了
select id, title from testgroup.m_blog union
select id, username from testgroup.m_user order by id;
DINSTINCT
我们再来试一下加上distinct,发现也是被去重的结果,emmmm,这个 字段貌似没什么用
select id, title from testgroup.m_blog union distinct
select id, username from testgroup.m_user order by id;