找到玩两场比赛的球员

发布于 2025-01-31 04:53:13 字数 600 浏览 4 评论 0原文

我想让那些同时下棋和跳棋的人的玩家。 我有一个这样的表:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

别忘他 2025-02-07 04:53:15

如果您想和谁同时下棋和跳棋一起获取playerid,那么查询将是:

SELECT playerid
FROM daily_player
WHERE game in ('checkers', 'chess') && website = 'www.abc.com' GROUP by playerid HAVING COUNT(*)=2;

If you want to fetch playerid with who played both chess and checkers, then the query will be:

SELECT playerid
FROM daily_player
WHERE game in ('checkers', 'chess') && website = 'www.abc.com' GROUP by playerid HAVING COUNT(*)=2;
梦里°也失望 2025-02-07 04:53:14

有多个子查询的长时间查询可能会做。

SELECT playerid FROM daily_player a
WHERE 
(SELECT COUNT(*) FROM daily_player WHERE game ='chess' AND playerid=a.`playerid`)=1 
AND
(SELECT COUNT(*) FROM daily_player WHERE game ='checkers' AND playerid=a.`playerid`)=1
AND website='www.abc.com'
GROUP BY playerid;

结果

playerid  
----------
a1        
b2 

说明:

  1. 第一个子查询:评估玩家是否参加比赛国际象棋。
  2. 第二个子查询:评估玩家是否玩游戏检查员。
  3. 通过使用,检查两个子量是否<代码> true 。
  4. 添加另一个来评估网站。

注意:如果每个玩家都可以多次玩单个游戏,则可以用= 1&gt; = 1替换。

A little long query with multiple sub queries may do.

SELECT playerid FROM daily_player a
WHERE 
(SELECT COUNT(*) FROM daily_player WHERE game ='chess' AND playerid=a.`playerid`)=1 
AND
(SELECT COUNT(*) FROM daily_player WHERE game ='checkers' AND playerid=a.`playerid`)=1
AND website='www.abc.com'
GROUP BY playerid;

RESULT

playerid  
----------
a1        
b2 

Explanation:

  1. 1st Subquery : Evaluate if the player played the game chess.
  2. 2nd Subquery : Evaluate if the player played the game checkers.
  3. Check if both subqueries are TRUE by using AND.
  4. Add another AND to evaluate the website.

Note : If every player can play a single game multiple times you can replace the =1 with >=1.

森林迷了鹿 2025-02-07 04:53:14

如果您不想要很长的查询,可以使用联接功能。像:

create table chess as select * from daily_player where game = 'chess';
create table checkers as select * from daily_player where game = 'checkers';

select chess.playerid from chess inner join checkers
on chess.playerid = checkers.playerid;

Could go for a join function if you don't want a really long query. Something like:

create table chess as select * from daily_player where game = 'chess';
create table checkers as select * from daily_player where game = 'checkers';

select chess.playerid from chess inner join checkers
on chess.playerid = checkers.playerid;
じ违心 2025-02-07 04:53:14

我通常在列中通过“ amph” 发现重复。

如果您只有两个游戏,这是最短的方法。

SELECT playerid
FROM daily_player
GROUP BY playerid 
HAVING COUNT(playerid ) > 1;    

I usually find duplicates in a column by "HAVING ".

if you have just two games this is shortest way.

SELECT playerid
FROM daily_player
GROUP BY playerid 
HAVING COUNT(playerid ) > 1;    
天暗了我发光 2025-02-07 04:53:14
select DISTINCT t1.player_id from daily_player as t1
        where exists (select 1 from daily_player as t2 where t1.player_id = t2.player_id and game = 'chess')
        and exists (select 1 from daily_player as t3 where t1.player_id = t3.player_id and game = 'checkers')
        and  website='abc.com'
select DISTINCT t1.player_id from daily_player as t1
        where exists (select 1 from daily_player as t2 where t1.player_id = t2.player_id and game = 'chess')
        and exists (select 1 from daily_player as t3 where t1.player_id = t3.player_id and game = 'checkers')
        and  website='abc.com'
小梨窩很甜 2025-02-07 04:53:13

使用时的问题是,还会发现每个条目仅满足您的两个条件之一,但您只想获得满足这两个条件的条件。使用时的问题(此答案似乎再次被删除了),因为游戏不能同时同时等于“国际象棋”和“检查器”,因此您将没有排。
因此,这意味着您将需要两个查询,并检查它们中的哪些玩家,一项检查game =“国际象棋”,一个用于游戏=“ Checker”。有不同的选择。一种可能性是在子句中使用

SELECT DISTINCT playerid
FROM daily_player
WHERE playerid IN (SELECT playerid FROM daily_player WHERE game = 'chess')
AND playerid IN (SELECT playerid FROM daily_player WHERE game = 'checkers')
AND website='abc.com';

另一种方法是存在:

SELECT DISTINCT playerid
FROM daily_player d1
WHERE EXISTS (SELECT 1 FROM daily_player WHERE game = 'chess' AND playerid = d1.playerid)
AND EXISTS (SELECT 1 FROM daily_player WHERE game = 'checkers' AND playerid = d1.playerid) 
AND website='abc.com'

请注意,只有在使用不同的情况下,只有在使用时才能实现您所需的结果。,并且不选择诸如游戏等的不同列。
如果您仍然想选择它们,可以当然可以这样做:

SELECT DISTINCT playerid,game,website
FROM daily_player
WHERE playerid IN (SELECT playerid FROM daily_player WHERE game = 'chess')
AND playerid IN (SELECT playerid FROM daily_player WHERE game = 'checkers')
AND website='abc.com';

但是,您将无法获得所显示的结果,但是在这种情况下,四行而不是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 using AND (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:

SELECT DISTINCT playerid
FROM daily_player
WHERE playerid IN (SELECT playerid FROM daily_player WHERE game = 'chess')
AND playerid IN (SELECT playerid FROM daily_player WHERE game = 'checkers')
AND website='abc.com';

Another way is using EXISTS:

SELECT DISTINCT playerid
FROM daily_player d1
WHERE EXISTS (SELECT 1 FROM daily_player WHERE game = 'chess' AND playerid = d1.playerid)
AND EXISTS (SELECT 1 FROM daily_player WHERE game = 'checkers' AND playerid = d1.playerid) 
AND website='abc.com'

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:

SELECT DISTINCT playerid,game,website
FROM daily_player
WHERE playerid IN (SELECT playerid FROM daily_player WHERE game = 'chess')
AND playerid IN (SELECT playerid FROM daily_player WHERE game = 'checkers')
AND website='abc.com';

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文