PostgreSQL:连接问题外连接 +子选择以比较值。
我正在使用 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 )
但我也想限制底部的最后一个“having”,所以它将是这样的,但当然它不起作用。
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 )
如何“保存”拥有之后的值,以便我也可以从底部进行比较? 谢谢大家。
I am using postresql but i am realy bad at construting sql queries.
I have this query and it works:
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 )
but i also want to limit the last "having" from the bottom so it will be something like this, but ofcourse it does not work.
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 )
how to "save" value that is after the having so i can compare it also from the bottom?
Thank you all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这主要是@wildplasser已经指出的
..减去
BETWEEN
的错误
..加上
JOIN
而不是IN
构造,这在 PostgreSQL 中通常更快。.. 更容易阅读
您对某些列进行了表限定,例如
pokerhands.site_id
,但不对其他列进行表限定,例如handhistories
,您可能需要清理它们。This is mostly what @wildplasser already pointed out
.. minus the mistake with
BETWEEN
.. plus
JOIN
instead ofIN
construct, which is usually faster in PostgreSQL... easier to read
You table-qualify some columns, like
pokerhands.site_id
, but not others, likehandhistories
, you might want to clean that up.BETWEEN 适合你吗?
(顺便说一句:丑陋的 SQL 语法,重复使用 AND 关键字)
更新:也可以用来简化查询的其余部分:
Would BETWEEN work for you ?
(BTW: ugly SQL syntax, reusing the AND keyword)
UPDATE: can also be used to simplify the rest of your query: