如何在一个表中包含多个select语句

问题描述 投票:0回答:1
Select count(Gender)  as 'FOScout' from stormtroopers_officer join st_officer_assign on stormtroopers_officer.STID=st_officer_assign.STID where Gender='Female';
SELECT count(Gender)  as 'FNScout' from stormtroopers_nco join st_nco_assign on stormtroopers_nco.STID=st_nco_assign.STID where Gender='Female';
SELECT count(Gender)  as 'FTScout' from stormtroopers_troop join st_troop_assign on stormtroopers_troop.STID=st_troop_assign.STID where Gender='Female';
Select count(Gender)  as 'MOScout' from stormtroopers_officer join st_officer_assign on stormtroopers_officer.STID=st_officer_assign.STID where Gender='male';
SELECT count(Gender)  as 'MNScout' from stormtroopers_nco join st_nco_assign on stormtroopers_nco.STID=st_nco_assign.STID where Gender='male';
SELECT count(Gender)  as 'MTScout' from stormtroopers_troop join st_troop_assign on stormtroopers_troop.STID=st_troop_assign.STID where Gender='male';
SELECT count(Gender)  as 'Total Female Scouts' from stormtroopers_troop WHERE Gender = 'Female';
SELECT count(Gender)  as 'Total Male Scouts' from stormtroopers_troop WHERE Gender='Male';
SELECT count(Gender)  as 'Total Male Scouts' from stormtroopers_troop;

当我执行它时,它显然会出现在单独的表中,而我想要的是将它们放在一张表中,如图所示

enter image description here

mysql sql database
1个回答
1
投票

将您的查询更改为:

SELECT
(Select count(Gender)  as 'FOScout' from stormtroopers_officer join st_officer_assign on stormtroopers_officer.STID=st_officer_assign.STID where Gender='Female') as 'FOScout',
(SELECT count(Gender)  as 'FNScout' from stormtroopers_nco join st_nco_assign on stormtroopers_nco.STID=st_nco_assign.STID where Gender='Female') as 'FNScout',
(SELECT count(Gender)  as 'FTScout' from stormtroopers_troop join st_troop_assign on stormtroopers_troop.STID=st_troop_assign.STID where Gender='Female') as 'FTScout',
(Select count(Gender)  as 'MOScout' from stormtroopers_officer join st_officer_assign on stormtroopers_officer.STID=st_officer_assign.STID where Gender='male') as 'MOScout',
(SELECT count(Gender)  as 'MNScout' from stormtroopers_nco join st_nco_assign on stormtroopers_nco.STID=st_nco_assign.STID where Gender='male') as 'MNScout',
(SELECT count(Gender)  as 'MTScout' from stormtroopers_troop join st_troop_assign on stormtroopers_troop.STID=st_troop_assign.STID where Gender='male') as 'MTScout',
(SELECT count(Gender)  as 'Total Female Scouts' from stormtroopers_troop WHERE Gender = 'Female') as 'Total Female Scouts',
(SELECT count(Gender)  as 'Total Male Scouts' from stormtroopers_troop WHERE Gender='Male') as 'Total Male Scouts',
(SELECT count(Gender)  as 'Total Male Scouts' from stormtroopers_troop) as 'Total Male Scouts'
© www.soinside.com 2019 - 2024. All rights reserved.