本篇内容主要讲解“mysql中怎么将几个没有关系的结果集放在一起”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“mysql中怎么将几个没有关系的结果集放在一起”吧!
解决方法:
需要将SELECT es.name primary_school ,COUNT(DISTINCT esi.stu_id) primary_num FROM……、SELECT es.name AS high_school,COUNT(DISTINCT esi.stu_id) AS high_num FROM……、SELECT COUNT(DISTINCT esi.stu_id) total_num FROM……的结果放到一起,由于结果之间没什么关系,先给获取到的结果添加编号,SELECT @rownumb:=@rownumb+1 AS rownum, resources.* FROM (SELECT @rownumb:=0) r,(……)resources,然后根据编号将三个结果集合并起来:
SELECT c.rownum,b.high_school,b.high_num,a.primary_school,a.primary_num, c.total_num
FROM
(……) b
LEFT JOIN
(……) a
ON a.rownum = b.rownum
LEFT JOIN
(……) c
ON c.rownum = b.rownum
这个结果如果刚好b里面的行数最多是没有问题的,但问题是不知道a、b哪个里面的行数最多,解决这个问题,如果可以用full join就可以直接解决了,但full join对于使用的版本不可用,采取的方法是用union all将几个left join后得到的结果合并在一起:
SELECT DISTINCT high_school,high_num,primary_school,primary_num, total_num
FROM
(SELECT c.rownum,b.high_school,b.high_num,a.primary_school,a.primary_num, c.total_num
FROM
(……) b
LEFT JOIN
(……) a
ON a.rownum = b.rownum
LEFT JOIN www.2cto.com
(……) c
ON c.rownum = b.rownum
UNION ALL
SELECT c.rownum,b.high_school,b.high_num,a.primary_school,a.primary_num, c.total_num
FROM
(……) a
LEFT JOIN
(……) b
ON a.rownum = b.rownum
LEFT JOIN
(……) c
ON c.rownum = a.rownum
) ee
到此,相信大家对“mysql中怎么将几个没有关系的结果集放在一起”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!