如何在 SELECT 语句中重命名和引用 COUNT(*)?
How to re-name and reference COUNT(*) in a SELECT statement?
我正在尝试在 SELECT 语句中使用 COUNT(*)。但是,我需要重命名它并能够在 WHERE 子句中引用它。
我试过使用 AS,也试过省略 AS,因为根据 oracle 页面似乎没有必要:https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions032.htm
尝试将新标识符括在引号中,但这也不起作用。
这可行,但是为计数提供一个 oracle 生成的名称,这并不理想,而且我不知道如何引用每一行的计数:
SELECT
school_name,
(SELECT COUNT(*)
FROM liason_to
WHERE school_name = s.school_name)
FROM school s;
这是我尝试过但行不通的方法:
SELECT
school_name,
(SELECT COUNT(*) AS numLiasons
FROM liason_to
WHERE school_name = s.school_name)
FROM school s
WHERE numLiasons > 0;
它没有生成列名 "numLiasons",最后的 where 子句不知道 numLiasons 是什么,所以失败了。
您可以使用连接和分组来避免子查询..并且您可以将您喜欢的 anme 分配为别名
SELECT s.school_name, COUNT(*) as my_count
FROM school s
INNER JOIN liason_to l on s.school_name = l.school_name
GROUP BY s.school_name
使用您的代码,您可以简单地在 (subselected ) 列分配一个别名
SELECT
school_name,
(SELECT COUNT(*)
FROM liason_to
WHERE school_name = s.school_name) as my_name
FROM school s;
无论如何要过滤一个聚合结果你可以使用但是 count(*)
请记住,这仅适用于非空行,因此通常 count(*) > 0
您不能在 WHERE 子句中引用别名,但您可以这样做:
SELECT
t.school_name,
t.numLiasons
FROM (
SELECT
s.school_name,
(
SELECT COUNT(*)
FROM liason_to
WHERE school_name = s.school_name
) AS numLiasons
FROM school s
) t
WHERE t.numLiasons > 0;
您可以通过简单地在列后面加上一个名称来为列添加别名,中间可以选择使用关键字 AS
。它与您已经对表格所做的基本相同。
SELECT school_name,
(SELECT count(*)
FROM liason_to l
WHERE l.school_name = s.school_name) AS numliasons
FROM school s;
或者干脆
SELECT school_name,
(SELECT count(*)
FROM liason_to l
WHERE l.school_name = s.school_name) numliasons
FROM school s;
但是您不能在 WHERE
子句中使用别名(别名是在 WHERE
子句中的条件选择了记录之后发生的)。你必须重复这个表达式。
SELECT school_name,
(SELECT count(*)
FROM liason_to l
WHERE l.school_name = s.school_name) numliasons
FROM school s
WHERE (SELECT count(*)
FROM liason_to l
WHERE l.school_name = s.school_name) > 0;
我正在尝试在 SELECT 语句中使用 COUNT(*)。但是,我需要重命名它并能够在 WHERE 子句中引用它。
我试过使用 AS,也试过省略 AS,因为根据 oracle 页面似乎没有必要:https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions032.htm
尝试将新标识符括在引号中,但这也不起作用。
这可行,但是为计数提供一个 oracle 生成的名称,这并不理想,而且我不知道如何引用每一行的计数:
SELECT
school_name,
(SELECT COUNT(*)
FROM liason_to
WHERE school_name = s.school_name)
FROM school s;
这是我尝试过但行不通的方法:
SELECT
school_name,
(SELECT COUNT(*) AS numLiasons
FROM liason_to
WHERE school_name = s.school_name)
FROM school s
WHERE numLiasons > 0;
它没有生成列名 "numLiasons",最后的 where 子句不知道 numLiasons 是什么,所以失败了。
您可以使用连接和分组来避免子查询..并且您可以将您喜欢的 anme 分配为别名
SELECT s.school_name, COUNT(*) as my_count
FROM school s
INNER JOIN liason_to l on s.school_name = l.school_name
GROUP BY s.school_name
使用您的代码,您可以简单地在 (subselected ) 列分配一个别名
SELECT
school_name,
(SELECT COUNT(*)
FROM liason_to
WHERE school_name = s.school_name) as my_name
FROM school s;
无论如何要过滤一个聚合结果你可以使用但是 count(*)
请记住,这仅适用于非空行,因此通常 count(*) > 0
您不能在 WHERE 子句中引用别名,但您可以这样做:
SELECT
t.school_name,
t.numLiasons
FROM (
SELECT
s.school_name,
(
SELECT COUNT(*)
FROM liason_to
WHERE school_name = s.school_name
) AS numLiasons
FROM school s
) t
WHERE t.numLiasons > 0;
您可以通过简单地在列后面加上一个名称来为列添加别名,中间可以选择使用关键字 AS
。它与您已经对表格所做的基本相同。
SELECT school_name,
(SELECT count(*)
FROM liason_to l
WHERE l.school_name = s.school_name) AS numliasons
FROM school s;
或者干脆
SELECT school_name,
(SELECT count(*)
FROM liason_to l
WHERE l.school_name = s.school_name) numliasons
FROM school s;
但是您不能在 WHERE
子句中使用别名(别名是在 WHERE
子句中的条件选择了记录之后发生的)。你必须重复这个表达式。
SELECT school_name,
(SELECT count(*)
FROM liason_to l
WHERE l.school_name = s.school_name) numliasons
FROM school s
WHERE (SELECT count(*)
FROM liason_to l
WHERE l.school_name = s.school_name) > 0;