我正在使用postresql,但我真的不擅长构造sql查询。我有一个查询,它可以工作:
SELECT handhistories FROM handhistories
JOIN pokerhands using (pokerhand_id)
JOIN gametypes using (gametype_id)
RIGHT OUTER JOIN playerhandscashkeycolumns using (pokerhand_id)
WHERE pokerhands.site_id=0
AND pokerhands.numberofplayers>=5 and pokerhands.numberofplayers<=7
AND (bigblind = 2 OR bigblind = 4 )
AND player_id in
(SELECT player_id FROM playerhandscashkeycolumns GROUP BY player_id
HAVING AVG(case didvpip when true then 100::real else 0 end) <= 20 )但我也想从底部限制最后一个“拥有”,所以它将是这样的,但它当然不起作用。
SELECT handhistories FROM handhistories
JOIN pokerhands using (pokerhand_id)
JOIN gametypes using (gametype_id)
RIGHT OUTER JOIN playerhandscashkeycolumns using (pokerhand_id)
WHERE pokerhands.site_id=0
AND pokerhands.numberofplayers>=5 and pokerhands.numberofplayers<=7
AND (bigblind = 2 OR bigblind = 4 )
AND player_id in
(SELECT player_id FROM playerhandscashkeycolumns GROUP BY player_id
HAVING AVG(case didvpip when true then 100::real else 0 end) <= 20
AND HAVING AVG(case didvpip when true then 100::real else 0 end) > 10 )如何“保存”save之后的值,这样我也可以从底部比较它?谢谢你们所有人。
发布于 2011-10-26 19:36:19
这主要是@wildplasser已经指出的
。。减去BETWEEN的错误
。。加上JOIN而不是IN构造,后者在PostgreSQL中通常更快。
。。更易读
SELECT handhistories
FROM handhistories
JOIN pokerhands USING (pokerhand_id)
JOIN gametypes USING (gametype_id)
RIGHT JOIN playerhandscashkeycolumns USING (pokerhand_id)
JOIN (
SELECT player_id
FROM playerhandscashkeycolumns
GROUP BY player_id
HAVING avg(CASE WHEN didvpip THEN 100::real ELSE 0 END) > 10
AND avg(CASE WHEN didvpip THEN 100::real ELSE 0 END) <= 20
) p USING (player_id)
WHERE pokerhands.site_id = 0
AND pokerhands.numberofplayers BETWEEN 5 AND 7
AND bigblind IN (2,4);您可以对某些列进行表限定化,如pokerhands.site_id,但不对其他列进行限定,如handhistories,您可能需要清理这些列。
发布于 2011-10-26 18:34:12
间歇期会对你有帮助吗?
HAVING AVG(case didvpip when true then 100::real else 0 end) BETWEEN 10 AND 20(顺便说一句:丑陋的SQL语法,重用AND关键字)
更新:还可以用来简化查询的其余部分:
AND pokerhands.numberofplayers BETWEEN 5 AND 7
AND bigblind IN ( 2, 4 )https://stackoverflow.com/questions/7901353
复制相似问题