我一直在试图编写一个SQL查询,这将返回重复的中奖彩票号码。
我有一个名为WinningLotteryNumbers的SQL表,以及一个名为WinningNumbers的列,其数据类型为vchar(14)
。我以这种格式存储WinningNumber:01-02-03-04-05
。
我的问题是这样,我说列中存有02-04-06-08-10
的WinningNumber。这5个数字很幸运,几天后再次抽出,但顺序不同,06-02-10-08-04
。如何查询列并返回所有相同但不同顺序的WinningNumbers。
我希望我的问题能像我自己的想法一样清晰。在此先感谢您的帮助!
答案 0 :(得分:3)
这是一种痛苦。让我建议这种方法:
select newWLN, count(*) as cnt
from (select LotteryNumber, group_concat(numpart order by numpart separator '-') as NewWLN
from (select wln.LotteryNumber,
substring_index(substring_index(wln.LotteryNumber, '-', n.n), '-', -1) as numpart
from WinningLotteryNumbers wln cross join
(select 1 as n union all select 2 union all select 3 union all select 4 union all select 5
) n
) wln
group by LotteryNumber
) nwln
group by newWLN
having count(*) > 1
order by count(*) desc;
这是一种蛮力方法。它将彩票号码分成每个彩票号码的五个组成部分。然后按顺序重新组合它们。最后一步是聚合来计算重复数。
答案 1 :(得分:1)
这只是一个想法。您可以拆分所有数字,按升序排序,然后再次存储在变量中。
然后,02-04-06-08-10将与06-02-10-08-04相同,一旦你对数字进行排序。
我希望这会有所帮助