我有一个用户DE9013,在 SQL 表中有两个正面评价:
# select * from pref_rep where id='DE9013';
id | author | good | fair | nice | about | last_rated | author_ip
--------+--------+------+------+------+-------+----------------------------+---------------
DE9013 | DE9241 | t | t | t | | 2011-03-06 09:23:00.400518 | 97.33.154.43
DE9013 | DE9544 | t | t | t | | 2011-03-06 10:06:37.561277 | 97.33.35.54
(2 rows)
而fair + nice评级的总和正如预期的那样是四个:
# select
count(nullif(r.fair, false)) +
count(nullif(r.nice, false)) -
count(nullif(r.fair, true)) -
count(nullif(r.nice, true))
from pref_rep r where id='DE9013';
?column?
----------
4
(1 row)
我的问题是:为什么我会在下面的列表中找到用户9013,我试图在其中找到所有玩过 30 多场完整游戏并且评分(一般+好)高于 30 的用户?
# select substring(m.id from 3)::bigint, 3
from pref_match m, pref_rep r
where m.id=r.id and
m.id like 'DE%'
group by m.id
having (sum(m.completed) > 30 and
count(nullif(r.fair, false)) +
count(nullif(r.nice, false)) -
count(nullif(r.fair, true)) -
count(nullif(r.nice, true)) > 30) limit 3;
substring | ?column?
-----------+----------
9013 | 3
8692 | 3
7059 | 3
(3 rows)
将 PostgreSQL 8.4.7 与 CentOS 5.7 / 64 位一起使用