SQL 添加具有值的额外列

SQL 添加具有值的额外列

我想在下面的计数列之外添加一个具有可选名称的额外列,我编写的代码如下,并显示结果如下

SELECT COUNT(ID) FROM TBL_STUDENTS_INFO where 
TBL_STUDENTS_INFO.come_from_university is not null
UNION ALL
SELECT COUNT(ID) FROM TBL_STUDENTS_GO
UNION ALL
SELECT COUNT(ID) FROM TBL_STUDENTS_TAJIL

    Result
    COUNT(ID)
    41
    88
    9

我希望代码显示这样的结果

Extra_Column      Counted
cnt_come_uni      41
cnt_go            88
cnt_tajil         9

答案1

我可以用下面的代码解决上述查询

SELECT 'come' label, to_char(COUNT(ID)) FROM TBL_STUDENTS_INFO where TBL_STUDENTS_INFO.come_from_university is not null
UNION ALL
SELECT 'go' label, to_char(COUNT(ID)) FROM TBL_STUDENTS_GO
UNION ALL
SELECT 'taji' label, to_char(COUNT(ID)) FROM TBL_STUDENTS_TAJIL

现在它显示如下,正如我所料

LABEL        NO of students
come          41
go            88
taji          9

相关内容