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