[英]SELECT COUNT(*) GROUP BY out zero rows
Tables in my db: 我的数据库中的表:
CREATE TABLE items (
id serial NOT NULL,
user_id integer NOT NULL,
status smallint
);
CREATE TABLE phones (
phone character varying,
users integer[]
);
My Query for find phone numbers where status = 1: 我的查询查找状态为1的电话号码:
SELECT phones.phone, COUNT(*) AS "count"
FROM phones,items
WHERE phones.phone = ANY (Array['7924445544', '8985545444'])
AND items.user_id = ALL (phones.users) AND items.status = 1
GROUP BY phones.phone;
Query out: 查询出:
phone | count
------------+------
7924445588 | 3
Need out with ZERO count: 需要零计数:
phone | count
------------+-------
8985545444 | 0
7924445544 | 3
How to get that? 如何获得?
You shouldn't do that in the query. 您不应该在查询中这样做。 However it is rather easy to do it if you want to:
但是,如果您要:
WITH phone_seek AS(
SELECT '8985545444' AS phone
UNION ALL
SELECT '7924445588 '
)
SELECT phone_seek.phone, COUNT(items.id) AS "count"
FROM
phones_seek
JOIN phones
ON phones_seek.phone = phones.phones
CROSS JOIN items
WHERE
items.user_id = ALL (phones.users) AND items.status = 1
GROUP BY phones.phone;
It's a bit tricky to create non-existing rows. 创建不存在的行有点棘手。 (There are billions of them, at least...) Do a
UNION ALL
with a select providing the result you want if that phone no doesn't exist. (至少有数十亿个……)如果没有该电话,请选择一个
UNION ALL
,以提供所需的结果。
<current query>
UNION ALL
select '8985545444',0
from one_row_table where not exists (select 1 from phones
where phones.phone = '8985545444')
EDIT: If the phone numbers do exists, but not fulfill the WHERE
clause conditions, use a correlated sub-select to do the count: 编辑:如果确实存在电话号码,但不满足
WHERE
子句条件,请使用相关的子选择进行计数:
SELECT phones.phone,
(select count(*) from items
where items.status = 1
and items.user_id = phones.users) as "Count"
FROM phones
WHERE phones.phone = ANY (Array['7924445544', '8985545444'])
Count is an aggregate function and generally db engines produces count 0 when no row matches the criteria. Count是一个聚合函数,通常,当没有行符合条件时,db引擎将产生count 0。
The reason why your query produces no results is inclusion of your phones.phone field in db and the group by. 您的查询未产生任何结果的原因是在db中包含了phone.phone字段,并在分组依据中包含了。
1) A cheap solution to this is select only count(*) as your application already be knowing the phone number: 1)一种便宜的解决方案是仅选择count(*),因为您的应用程序已经知道电话号码:
SELECT COUNT(*) AS "count"
FROM phones, items
WHERE phones.phone = '8985545444'
AND items.user_id = ALL (phones.users) AND items.status = 1 ;
2) An ideal solution to this would be your application to handle 0 result returned by db. 2)一个理想的解决方案是您的应用程序处理db返回的0结果。
3) And if you need db to do all job you may use something like: 3)如果您需要db来完成所有工作,则可以使用以下方法:
SELECT phones.phone, COUNT(*) AS "count"
FROM phones, items
WHERE phones.phone = '8985545444'
AND items.user_id = ALL (phones.users) AND items.status = 1
GROUP BY phones.phone;
UNION ALL
select '8985545444',0
from one_row_table where not exists (select 1 from phones
where phones.phone = '8985545444')
I'm not well versed with PostreSQL's array syntax, but this seems to be a simple outer join: 我不太熟悉PostreSQL的数组语法,但这似乎是一个简单的外部联接:
SELECT phones.phone, COUNT(items.user_id) AS "count"
FROM phones LEFT JOIN items
ON items.user_id = ALL (phones.users)
AND items.status = 1
WHERE phones.phone = ANY (Array['7924445544', '8985545444'])
GROUP BY phones.phone;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.