从 mysql 中的一手牌中找出两对
find a two pair from a poker hand in mysql
嗨,我需要帮助来弄清楚如何从一手牌中找到两对。
我相信我需要计算不同卡片的数量,然后根据以下逻辑告诉我它是否是两对,即两对是一手扑克牌,包含两张相同等级的牌,两张另一等级的牌和一张牌第三等级;我只是不确定该怎么做。
感谢任何帮助。
这是我的扑克牌table
+----------+------+------+------+-----------+-----------+
| cardName | face | type | suit | faceValue | gameValue |
+----------+------+------+------+-----------+-----------+
| AC | no | A | C | 1 | 14 |
| 2C | no | 2 | C | 2 | 2 |
| 3C | no | 3 | C | 3 | 3 |
| 4C | no | 4 | C | 4 | 4 |
| 5C | no | 5 | C | 5 | 5 |
+----------+------+------+------+-----------+-----------+
和扑克牌手
+----------+--------+----+-----+----+----+----+----------+
| playerId | gameId | C1 | C2 | C3 | C4 | C5 | handType |
+----------+--------+----+-----+----+----+----+----------+
| 12789 | 17MET | QH | QS | 3D | 3C | 3H | |
| 12789 | 82SAT | 7C | 4S | 4D | 4C | 3H | |
| 56347 | 03DEC | 6S | 3S | 3H | 3C | 3D | |
| 56347 | 23WSA | KH | 10H | 7H | 3H | AH | |
| 56347 | 30DEC | AC | KH | KD | 3D | 3S | |
+----------+--------+----+-----+----+----+----+----------+
我需要获取最后一行
+----------+--------+----+-----+----+----+----+----------+
| playerId | gameId | C1 | C2 | C3 | C4 | C5 | handType |
+----------+--------+----+-----+----+----+----+----------+
| 56347 | 30DEC | AC | KH | KD | 3D | 3S | |
+----------+--------+----+-----+----+----+----+----------+
正如我在评论中所说,用适合此类事情的语言来完成要好得多。 SQL 不是完成这项工作的正确工具。仅作为学术练习,这是您需要的声明:
select *
from pokerCard
where (left(c1,1) = left(c2,1) and left(c3,1) = left(c4,1))
or (left(c1,1) = left(c2,1) and left(c3,1) = left(c5,1))
or (left(c1,1) = left(c2,1) and left(c4,1) = left(c5,1))
or (left(c1,1) = left(c3,1) and left(c2,1) = left(c4,1))
or (left(c1,1) = left(c3,1) and left(c2,1) = left(c5,1))
or (left(c1,1) = left(c3,1) and left(c4,1) = left(c5,1))
or (left(c1,1) = left(c4,1) and left(c2,1) = left(c3,1))
or (left(c1,1) = left(c4,1) and left(c2,1) = left(c5,1))
or (left(c1,1) = left(c4,1) and left(c3,1) = left(c5,1))
or (left(c1,1) = left(c5,1) and left(c2,1) = left(c3,1))
or (left(c1,1) = left(c5,1) and left(c2,1) = left(c4,1))
or (left(c1,1) = left(c5,1) and left(c3,1) = left(c4,1))
or (left(c2,1) = left(c3,1) and left(c1,1) = left(c4,1))
or (left(c2,1) = left(c3,1) and left(c1,1) = left(c5,1))
or (left(c2,1) = left(c3,1) and left(c4,1) = left(c5,1))
or (left(c2,1) = left(c4,1) and left(c3,1) = left(c5,1))
or (left(c2,1) = left(c5,1) and left(c3,1) = left(c4,1))
我会通过 UNION 将每张牌作为预聚合来处理,以获得普通牌,而不管花色如何。然后申请一个组...
select PlayerID, GameID, left( c1,1 ) as OneCard
from PlayerHand
union all
select PlayerID, GameID, left( c2,1 ) as OneCard
from PlayerHand
union all
select PlayerID, GameID, left( c3,1 ) as OneCard
from PlayerHand
union all
select PlayerID, GameID, left( c4,1 ) as OneCard
from PlayerHand
union all
select PlayerID, GameID, left( c5,1 ) as OneCard
from PlayerHand
这会给你一个类似下面的东西 person/game
playerid gameid onecard
12789 17MET Q
12789 17MET Q
12789 17MET 3
12789 17MET 3
12789 17MET 3
现在,您可以轻松查看卡片并进行简单聚合
select
preQuery.playerid,
preQuery.gameid,
preQuery.onecard,
count(*) as CntThisCard
from
( the entire union query above ) preQuery
group by
preQuery.playerid,
preQuery.gameid,
preQuery.onecard
having
count(*) > 1
根据您的数据,这将 return 以下行...
playerid gameid onecard cntThisCard
12789 17MET Q 2
12789 17MET 3 3 This is a full-house
12789 82SAT 4 3 Three-of-a-kind
56347 03DEC 3 4 Four-of-a-kind
56347 23WSA (not returned in data set)
56347 30DEC K 2
56347 30DEC 3 2 Two-pair
那么现在,如何提取任何内容 "hand" 这也会卷起来...
select
QryLvl2.PlayerID,
QryLvl2.GameID
from
( the entire query above returning per-card count ) QryLvl2
where
QryLvl2.CntThisCard = 2
group by
QryLvl2.PlayerID,
QryLvl2.GameID
having
count(*) = 2
在这种情况下,由于您要明确地寻找两对,所以我有 where 子句明确地只寻找手中有 2 的牌。计数 (*) = 2 的组表示两张不同的牌,它们会给你最后一手牌。
但是从第二张可以看出,你也可以立即识别出更好的4张、葫芦、3张、2对和单高牌。
然后您可以将牌 table 简化为 number/face 以确定一对 Jacks/3 是比 10 和 9 更高的牌,因为您不在乎关于牌的花色,只是与其他手牌相比的面值。
嗨,我需要帮助来弄清楚如何从一手牌中找到两对。 我相信我需要计算不同卡片的数量,然后根据以下逻辑告诉我它是否是两对,即两对是一手扑克牌,包含两张相同等级的牌,两张另一等级的牌和一张牌第三等级;我只是不确定该怎么做。
感谢任何帮助。
这是我的扑克牌table
+----------+------+------+------+-----------+-----------+
| cardName | face | type | suit | faceValue | gameValue |
+----------+------+------+------+-----------+-----------+
| AC | no | A | C | 1 | 14 |
| 2C | no | 2 | C | 2 | 2 |
| 3C | no | 3 | C | 3 | 3 |
| 4C | no | 4 | C | 4 | 4 |
| 5C | no | 5 | C | 5 | 5 |
+----------+------+------+------+-----------+-----------+
和扑克牌手
+----------+--------+----+-----+----+----+----+----------+
| playerId | gameId | C1 | C2 | C3 | C4 | C5 | handType |
+----------+--------+----+-----+----+----+----+----------+
| 12789 | 17MET | QH | QS | 3D | 3C | 3H | |
| 12789 | 82SAT | 7C | 4S | 4D | 4C | 3H | |
| 56347 | 03DEC | 6S | 3S | 3H | 3C | 3D | |
| 56347 | 23WSA | KH | 10H | 7H | 3H | AH | |
| 56347 | 30DEC | AC | KH | KD | 3D | 3S | |
+----------+--------+----+-----+----+----+----+----------+
我需要获取最后一行
+----------+--------+----+-----+----+----+----+----------+
| playerId | gameId | C1 | C2 | C3 | C4 | C5 | handType |
+----------+--------+----+-----+----+----+----+----------+
| 56347 | 30DEC | AC | KH | KD | 3D | 3S | |
+----------+--------+----+-----+----+----+----+----------+
正如我在评论中所说,用适合此类事情的语言来完成要好得多。 SQL 不是完成这项工作的正确工具。仅作为学术练习,这是您需要的声明:
select *
from pokerCard
where (left(c1,1) = left(c2,1) and left(c3,1) = left(c4,1))
or (left(c1,1) = left(c2,1) and left(c3,1) = left(c5,1))
or (left(c1,1) = left(c2,1) and left(c4,1) = left(c5,1))
or (left(c1,1) = left(c3,1) and left(c2,1) = left(c4,1))
or (left(c1,1) = left(c3,1) and left(c2,1) = left(c5,1))
or (left(c1,1) = left(c3,1) and left(c4,1) = left(c5,1))
or (left(c1,1) = left(c4,1) and left(c2,1) = left(c3,1))
or (left(c1,1) = left(c4,1) and left(c2,1) = left(c5,1))
or (left(c1,1) = left(c4,1) and left(c3,1) = left(c5,1))
or (left(c1,1) = left(c5,1) and left(c2,1) = left(c3,1))
or (left(c1,1) = left(c5,1) and left(c2,1) = left(c4,1))
or (left(c1,1) = left(c5,1) and left(c3,1) = left(c4,1))
or (left(c2,1) = left(c3,1) and left(c1,1) = left(c4,1))
or (left(c2,1) = left(c3,1) and left(c1,1) = left(c5,1))
or (left(c2,1) = left(c3,1) and left(c4,1) = left(c5,1))
or (left(c2,1) = left(c4,1) and left(c3,1) = left(c5,1))
or (left(c2,1) = left(c5,1) and left(c3,1) = left(c4,1))
我会通过 UNION 将每张牌作为预聚合来处理,以获得普通牌,而不管花色如何。然后申请一个组...
select PlayerID, GameID, left( c1,1 ) as OneCard
from PlayerHand
union all
select PlayerID, GameID, left( c2,1 ) as OneCard
from PlayerHand
union all
select PlayerID, GameID, left( c3,1 ) as OneCard
from PlayerHand
union all
select PlayerID, GameID, left( c4,1 ) as OneCard
from PlayerHand
union all
select PlayerID, GameID, left( c5,1 ) as OneCard
from PlayerHand
这会给你一个类似下面的东西 person/game
playerid gameid onecard
12789 17MET Q
12789 17MET Q
12789 17MET 3
12789 17MET 3
12789 17MET 3
现在,您可以轻松查看卡片并进行简单聚合
select
preQuery.playerid,
preQuery.gameid,
preQuery.onecard,
count(*) as CntThisCard
from
( the entire union query above ) preQuery
group by
preQuery.playerid,
preQuery.gameid,
preQuery.onecard
having
count(*) > 1
根据您的数据,这将 return 以下行...
playerid gameid onecard cntThisCard
12789 17MET Q 2
12789 17MET 3 3 This is a full-house
12789 82SAT 4 3 Three-of-a-kind
56347 03DEC 3 4 Four-of-a-kind
56347 23WSA (not returned in data set)
56347 30DEC K 2
56347 30DEC 3 2 Two-pair
那么现在,如何提取任何内容 "hand" 这也会卷起来...
select
QryLvl2.PlayerID,
QryLvl2.GameID
from
( the entire query above returning per-card count ) QryLvl2
where
QryLvl2.CntThisCard = 2
group by
QryLvl2.PlayerID,
QryLvl2.GameID
having
count(*) = 2
在这种情况下,由于您要明确地寻找两对,所以我有 where 子句明确地只寻找手中有 2 的牌。计数 (*) = 2 的组表示两张不同的牌,它们会给你最后一手牌。
但是从第二张可以看出,你也可以立即识别出更好的4张、葫芦、3张、2对和单高牌。
然后您可以将牌 table 简化为 number/face 以确定一对 Jacks/3 是比 10 和 9 更高的牌,因为您不在乎关于牌的花色,只是与其他手牌相比的面值。