找到玩两场比赛的球员
我想让那些同时下棋和跳棋的人的玩家。 我有一个这样的表:
playerid game website
a1 chess www.abc.com
a2 checkers www.cba.com
a1 checkers www.abc.com
b2 chess www.abc.com
b1 chess www.abc.com
a3 checkers www.aaa.com
b2 checkers www.abc.com
所需的输出(A3和B1被排除在外),
a1
b2
select game, playerid, website
from player_db
where (game= 'chess' or game= 'checkers') and website='abc.com'
group by playerid ;
这是我的SQL,但似乎并没有得到这两个游戏 - 它选择了国际象棋或棋子。
I want to get the playerid of those who played both chess and checkers.
I have a table that's like this:
playerid game website
a1 chess www.abc.com
a2 checkers www.cba.com
a1 checkers www.abc.com
b2 chess www.abc.com
b1 chess www.abc.com
a3 checkers www.aaa.com
b2 checkers www.abc.com
Desired output (a3 and b1 should be excluded)
a1
b2
select game, playerid, website
from player_db
where (game= 'chess' or game= 'checkers') and website='abc.com'
group by playerid ;
This is my SQL, but it doesn't seem to be getting both of the games -- it selects either chess or checkers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您想和谁同时下棋和跳棋一起获取playerid,那么查询将是:
If you want to fetch playerid with who played both chess and checkers, then the query will be:
有多个子查询的长时间查询可能会做。
结果
说明:
和
,检查两个子量是否<代码> true 。和
来评估网站。注意:如果每个玩家都可以多次玩单个游戏,则可以用
= 1
用&gt; = 1
替换。A little long query with multiple sub queries may do.
RESULT
Explanation:
TRUE
by usingAND
.AND
to evaluate the website.Note : If every player can play a single game multiple times you can replace the
=1
with>=1
.如果您不想要很长的查询,可以使用联接功能。像:
Could go for a join function if you don't want a really long query. Something like:
我通常在列中通过“ amph” 发现重复。
如果您只有两个游戏,这是最短的方法。
I usually find duplicates in a column by "HAVING ".
if you have just two games this is shortest way.
使用
或
时的问题是,还会发现每个条目仅满足您的两个条件之一,但您只想获得满足这两个条件的条件。使用和
时的问题(此答案似乎再次被删除了),因为游戏不能同时同时等于“国际象棋”和“检查器”,因此您将没有排。因此,这意味着您将需要两个查询,并检查它们中的哪些玩家,一项检查game =“国际象棋”,一个用于游戏=“ Checker”。有不同的选择。一种可能性是在子句中使用
:
另一种方法是存在:
请注意,只有在使用
不同的情况下,只有在使用
时才能实现您所需的结果。
,并且不选择诸如游戏等的不同列。如果您仍然想选择它们,可以当然可以这样做:
但是,您将无法获得所显示的结果,但是在这种情况下,四行而不是2行。如果您删除上面的选择游戏,则会得到再次两个行,因为网站没有差异。
The problem when using
OR
is that also every entry will be found that satisfies only one of your two conditions, but you want to get only those which meet both conditions. The problem when usingAND
(this answer seems to have been deleted again) will be you will get no rows since a game can't equal "chess" and "checker" the same time.So, this means you will need two queries and to check which players occur in both of them, one checking for game = "chess" and one for game="checker". There are different options to do this. One possibility is to use an
IN
clause:Another way is using
EXISTS
:Please note that your desired outcome of "a1 b2" can only be achieved when using
DISTINCT
and not selecting differing columns like game etc.In case you want to still select them you can of course do this:
But then you will not get the result you have shown, but in this case four lines instead of 2. If you remove the game of the select above, you will get two rows again because the website does not differ.