今天在项目中遇到一个数据库查询的问题:三张表分别放置不同的东西:分享的音频相关数据、分享的文字图片说说、分享的主题相关数据。所有分享的东西都可看做新鲜事,现在要求从这三张表将相同的几个字段的数据全部查找出来按照发布时间先后排序(至于为什么不把这三张表的数据整到一张表里面这是由于数据库是这样设计的,咱这先不讨论数据库设计的好坏,就记录一下自己怎么使用三表联合查询将这三张表的数据都查找出来)。
在网上找了些联合查询相关的资料:
有几个不错的技术博客做了很好的介绍:
1、http://blog.sina.com.cn/s/blog_6ad62438010168lg.html
2、http://www.ynpxrz.com/n598810c2024.aspx
介绍:使用union或union all都可实现合并两个或多个select语句的结果集:其中union会将查出来的结果中相同内容的行合并,而union all不会合并相同行。
要点:
(1)两次或多次查询的列数必须一致;
(2)以第一个子查询语句中列的类型为标准,之后所有查询语句中各列的类型第一个子查询中对应类的类型一致;
(3)多次select语句中查询的列名可以不相同,最后查询结果表的列名是以第一个子查询语句中的列名来命名的;
(4)如果子句中有order by,limit,需要用括号()包起来。如果将order by,limit放到所有子句之后,即对最终合并的结果表进行排序或筛选。
操作:
1、参考上面给出的两个技术博文,我自己进行了实践,前面几次都出错,我下面给出我敲出来的sql已经相应的报错内容,并附上截图:
sql语句1: mysql> select * from (select alarm_publishSite,alarm_publishtime from alarm union select theme_issueLocation,theme_publishtime from theme);
报错:ERROR 1248 (42000): Every derived table must have its own alias
这个错误是指每个派生表需要一个别名
sql语句2:mysql> select * from (select alarm_publishSite,alarm_publishtime from alarm) as t1 union (select theme_issueLocation,theme_publishtime from theme) as t2;
报错:ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as t2' at line 1
sql语句3:mysql> select * from (select alarm_publishSite,alarm_publishtime from alarm) as t1 union (select theme_issueLocation,theme_publishtime from theme);
这次很幸运,成功将两个表中的数据联合查出来了,如下图:
sql2语句与sql3语句之间的区别你可以比较得出。
sql语句4:mysql> select * from (select alarm_publishSite,alarm_publishtime from alarm) as t1 union (select theme_issueLocation,theme_publishtime from theme) as t2 union (select subject_issueLocation,subject_time from subject);
报错:ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as t2 union (select subject_issueLocation,subject_time from subject)' at line 1
这个报错是指 ”as t2 union (select subject_issueLocation,subject_time from subject)“这部分附近有语法问题。
sql语句5:mysql> select * from (select alarm_publishSite,alarm_publishtime from alarm) as t1 union (select theme_issueLocation,theme_publishtime from theme) union (select subject_issueLocation,subject_time from subject);
得出结果,如下图:
sql4语句与sql5语句之间的区别你可以比较得出。
sql语句6:mysql> select * from (select alarm_publishSite as location,alarm_publishtime as time from alarm) as t1 union (select theme_issueLocation,theme_publishtime from theme) union (select subject_issueLocation,subject_time from subject) order by time desc;
得出查询结果,如下图:
在sql6语句中,主要就是给第一个子查询中查询的列名去了相应的别名,最后可以看到,查询出来的结果表中的字段名就成了刚取的别名了。