How to put irrelevant result sets together in mysql

Source: Internet
Author: User

When I was doing A report yesterday, I encountered the requirement to horizontally display data of type A and data of type B in A table. There is no relationship between the two, and the amount of data is too large to be determined, after a while, the following solution is created: www.2cto.com needs to 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 ...... 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 www.2cto.com (......) 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
Author LemonYang

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.