在扑克手中找到四种

时间:2017-02-15 21:48:39

标签: mysql

这是我的表格

pokerHands
+----------+--------+----+-----+----+----+----+
| playerId | gameId | R1 | R2  | R3 | R4 | R5 |
+----------+--------+----+-----+----+----+----+
|    12789 | 17MET  | QH | QS  | 3D | 3C | 3H |
|    12789 | 82SAT  | 7C | 4S  | 4D | 4C | 3H |
|    56347 | 03DEC  | 3S | 6S  | 3H | 3C | 3D |
|    56347 | 23WSA  | KH | 10H | 7H | 3H | AH |
|    84643 | 78GUV  | 9H | 7C  | 5H | 5C | 2S |
|    90734 | 65YOB  | QH | JH  | 8C | 8S | 8D |
+----------+--------+----+-----+----+----+----+

select * from pokerCards limit 5;
+----------+------+------+------+-----------+-----------+
| cardName | face | type | suit | faceValue | gameValue |
+----------+------+------+------+-----------+-----------+
| 10C      | no   | 10   | C    |        10 |        10 |
| 10D      | no   | 10   | D    |        10 |        10 |
| 10H      | no   | 10   | H    |        10 |        10 |
| 10S      | no   | 10   | S    |        10 |        10 |
| 2C       | no   | 2    | C    |         2 |         2 |
+----------+------+------+------+-----------+-----------+

playerId和gameId是第一个表pokerHands中的复合PRIMARY KEYS,而cardName是第二个表的PRIMARY KEY。

我正在尝试找出如何从第一个表中选择一个将从当前pokerHands表中返回的四个

+----------+--------+----+-----+----+----+----+
| playerId | gameId | R1 | R2  | R3 | R4 | R5 |
+----------+--------+----+-----+----+----+----+
|    56347 | 03DEC  | 3S | 6S  | 3H | 3C | 3D |
+----------+--------+----+-----+----+----+----+

任何帮助都非常感激。

2 个答案:

答案 0 :(得分:0)

试试这个:

select playerId, gameId, R1, R2, R3, R4, R5 from pokerHands as ph
join pokerCards as pc1 on pc1.cardName=ph.R1
join pokerCards as pc2 on pc2.cardName=ph.R2
join pokerCards as pc3 on pc3.cardName=ph.R3
join pokerCards as pc4 on pc4.cardName=ph.R4
join pokerCards as pc5 on pc5.cardName=ph.R5
where ( (pc1.type=pc2.type) and (pc1.type=pc3.type) and (pc1.type=pc4.type) )
or ( (pc1.type=pc2.type) and (pc1.type=pc3.type) and (pc1.type=pc5.type) )
or ( (pc1.type=pc2.type) and (pc1.type=pc4.type) and (pc1.type=pc5.type) )
or ( (pc1.type=pc3.type) and (pc1.type=pc4.type) and (pc1.type=pc5.type) )
or ( (pc2.type=pc3.type) and (pc2.type=pc4.type) and (pc2.type=pc5.type) )

答案 1 :(得分:0)

如果您按照等级顺序将卡插入R1-R5,您的生活会更容易。然后你可以检查两种情况(卡片是左边还是右边)

rank(r1)==rank(r2) and rank(r1)==rank(r3) and rank(r1)==rank(r4)
OR
rank(r2)==rank(r3) and rank(r2)==rank(r4) and rank(r2)==rank(r5)
相关问题