我正在尝试解决彩票号码问题。有一张桌子 card ,上面有一张彩票号码:
表:卡
+----+----+----+----+----+----+
| ID | b1 | b2 | b3 | b4 | b5 |
+----+----+----+----+----+----+
| 1 | 10 | 12 | 14 | 15 | 17 |
| 2 | 01 | 13 | 30 | 44 | 50 |
| 3 | 15 | 14 | 28 | 46 | 60 |
| 4 | 09 | 10 | 32 | 47 | 59 |
| 5 | 14 | 10 | 25 | 47 | 58 |
比赛是带球的。首先,我们对一个球(12)进行排序,然后对另一个球(15)进行排序,如下所示:
12, 15, 17, 30, 32, 40, 2, 14
我需要选择第一行,如果下一球是10,它将成为获胜者。
我不知道该怎么说英语,但是我需要一个需要一个数字才能成为赢家的卡状态。
今天,我的sql就是这样(尝试所有可能性):
SELECT id FROM card WHERE b1 IN (12, 15, 17, 30, 32, 40, 2, 14) AND
b2 IN (12, 15, 17, 30, 32, 40, 2, 14) ... OR
b2 IN (12, 15, 17, 30, 32, 40, 2, 14) AND b3...
有人可以帮忙吗?谢谢!
答案 0 :(得分:2)
最好的方法是创建一个排序球表,然后查询排序的表,如下所示:
SELECT id FROM card WHERE b1 IN (SELECT ball FROM balls)
AND b2 IN (SELECT ball FROM balls)
AND b3 IN (SELECT ball FROM balls)
AND b4 IN (SELECT ball FROM balls)
AND b5 IN (SELECT ball FROM balls)
每次排序后,您将球号保存在球表中:
INSERT INTO balls(ball) VALUES (10);
答案 1 :(得分:1)
我会垂直记录您的选择:
这也是SQL Server的答案,因此格式可能会略有偏离。
--Correct balls
declare @b1 int =2
,@b2 int = 10
,@b3 int = 15
,@b4 int = 21
,@b5 int = 45
declare @t as table (ID int, pick int)
insert into @t
values
(1,10),(1,12),(1,14),(1,15),(1,17)
,(2,1),(2,13),(2,30),(2,44),(2,50)
--This gives you the number right per ID
--I commented out the answer for 4 or more to get you results
select ID, sum(case when pick in (@b1,@b2,@b3,@b4,@b5) then 1 else 0 end)
from @t
group by id
--having sum(case when pick in (@b1,@b2,@b3,@b4,@b5) then 1 else 0 end) >=4
这是2对1的答案...
要正确获取数据,请使用此表作为选择表。当我说对时,我的意思是归一化,这使该游戏可以让您在不改变桌子结构的情况下选择无限量的球。
Select ID
,
A.pick
From picks
Cross apply (values (b1),(b2),(b3),(b4),(b5)) a(pick)
对不起,格式。通过电话做到这一点
答案 2 :(得分:1)
在这种情况下,键值结构使解决问题变得更加容易。您实际上并不关心值所在的列,即使id具有与其关联的值也是如此。我认为MySQL没有取消旋转功能,但是您可以手动轻松地做到这一点:
SELECT id
FROM (SELECT id, 1 AS rw, b1 AS VALUE FROM card
UNION ALL
SELECT id, 2 AS rw, b2 AS VALUE FROM card
UNION ALL
SELECT id, 3 AS rw, b3 AS VALUE FROM card
UNION ALL
SELECT id, 4 AS rw, b4 AS VALUE FROM card
UNION ALL
SELECT id, 5 AS rw, b5 AS VALUE FROM card) v
WHERE VALUE IN (12,
15,
17,
30,
32,
40,
2,
14)
GROUP BY id
HAVING COUNT (*) = 4
(此查询已在Oracle而非MySQL上进行了测试,因此语法可能不正确。)
答案 3 :(得分:0)
我建议使用PIVOT功能。那么B1的所有值都在同一行中。 b2,b3,b4,b5 ...