How to put irrelevant result sets together in MySQL

Source: Internet
Author: User
In MySQL, how does one put a few irrelevant result sets together? The problem is that the number of rows in B is the most, but the problem is that a and B have the most rows.

In MySQL, how does one put a few irrelevant result sets together? The problem is that the number of rows in B is the most, but the problem is that a and B have the most rows.

When you make A report, if you want to horizontally display data of type A and data of type B in A table, there is no relationship between the two, and the data volume of which is too large is not determined, after a while, the following solutions were created:

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, 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


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 unknown, if you can use full join, you can solve it directly, but full join is not available for the mysql version in use, the method is to combine the results obtained after several left joins using union all:

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
(......) 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

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.