How to put irrelevant result sets in mysql _ MySQL
Source: Internet
Author: User
In mysql, how do I put A few irrelevant result sets together bitsCN.com when I was doing A report yesterday? when I encountered the requirement to put data of type A and data of type B in A table horizontally, there is no relationship between the two, and the data volume is too large to be determined. after a while, the following solution is created: SELECT es is required. 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 ...... AS there is no relationship between the results, first add the number to the obtained results, SELECT @ rownumb: = @ rownumb + 1 AS rownum, resources. * FROM (SELECT @ rownumb: = 0) r ,(......) Resources, and then combine the three results according to the number: SELECT c. rownum, B. high_school, B. high_num,. primary_school,. primary_num, c. total_numFROM (......) BLEFT JOIN (......) A ON a. rownum = B. rownumLEFT JOIN (......) C ON c. rownum = B. rownum: if the result is that the number of rows in B is correct, the problem is that the number of rows in a and B is the largest. to solve this problem, if you can use full join, you can solve it directly, but full join is not available for the mysql version in use, the union all clause is used to merge the results obtained after several left joins: select distinct high_school, high_num, primary_school, primary_num, total_num FROM (SELECT c. rownum, B. high_school, B. high_num,. primary_school,. primary_num, c. total_num FROM (......) B LEFT JOIN (......) A ON a. rownum = B. rownum LEFT JOIN (......) C ON c. rownum = B. rownumUNION 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
LemonYangbitsCN.com
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service