为 MYSQL 长文本列中彩票号码的出现次数创建一个计数器。这可能需要 PHP

发布于 2024-12-14 12:35:36 字数 1168 浏览 6 评论 0原文

我可能需要 PHP 或其他语言来完成这项工作,但直接使用 MYSQL 的解决方案是首选。

我整理了去年纽约州彩票中奖号码的清单。我在 MYSQL 中创建了一个 4 列表来存储数据。一项用于日期,一项用于中奖号码,一项用于奖金号码,一项用于支付。在对如何存储彩票号码(我将其视为整数数组)进行了一些研究后,我发现了使用长文本数据类型的建议。输入数据时用管道分隔每个值,如下所示:01|34|35|36|56|59。这些数字始终按升序排列,给出两位数,并且不超过 59。

我的流程的下一步是创建一个表格,显示每个数字出现的频率。这是我的伪代码:

1 Create table "Number_Frequency"
2 Create column "Number"(Type int)
3 Create column "Frequency"(Type int, initial value 0)
4 Look at table "new_table"
5 Look at column "numbers"
6 Look at first row
7 Look at first two digits
  7A If this two digit number isn't in the column "Number" in the table "Number_Frequency", add to column "Number" in table "Number_Frequency" and set corresponding "Frequency" to 1
  7B Else if number is already in column "Number" increase the value of frequency by 1
8 Look for a pipe symbol
  8A If there is a pipe symbol, repeat all parts of step 7 for the next two digits after the pipe symbol.
  8B Else if there is another row, look at the first two digits of that row, and start from step 7A
  8C Else terminate.

正如你所看到的,我非常清楚我想要做什么。我有几个简单的问题要问:这可以专门在 MYSQL 中完成吗?更重要的是,是否有一个重要的、明显的理由让人不应该费心去尝试?如果有的话,原因是什么?

I may need PHP or some other language to do this job, though A solution in MYSQL directly is preferred.

I've gotten together a list of the winning lottery numbers for the New York state lottery for the last year. I've created a 4 column table in MYSQL to store the data. One for Date, one for the winning lottery number, one for the bonus number, and one for the payout. After doing a little research on how to store the lottery numbers, which I considered an array of ints, I found a recommendation to use the longtext data type. The data is entered with a pipe separating each value like this: 01|34|35|36|56|59. The numbers are always in ascending order, are given two digits, and do not go above 59.

The next step in my process is to create a table that shows how often each number happens. Here is my pseudo code for this thing:

1 Create table "Number_Frequency"
2 Create column "Number"(Type int)
3 Create column "Frequency"(Type int, initial value 0)
4 Look at table "new_table"
5 Look at column "numbers"
6 Look at first row
7 Look at first two digits
  7A If this two digit number isn't in the column "Number" in the table "Number_Frequency", add to column "Number" in table "Number_Frequency" and set corresponding "Frequency" to 1
  7B Else if number is already in column "Number" increase the value of frequency by 1
8 Look for a pipe symbol
  8A If there is a pipe symbol, repeat all parts of step 7 for the next two digits after the pipe symbol.
  8B Else if there is another row, look at the first two digits of that row, and start from step 7A
  8C Else terminate.

As you can see I have a very clear idea of what I want to do. I have a few simple questions for SO: Can this be done exclusively in MYSQL? And more importantly, is there a major, glaring reason why one shouldn't bother trying? If there is, what is that reason?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

满栀 2024-12-21 12:35:36

将一组固定大小的数字存储为连接的字符串是疯狂的。

考虑将它们存储在 6 个独立的列中,每个列都是整数类型。或者,将它们存储在只有两列的表中:draw_idvalue;每次抽奖此表中有 6 个条目。然后,构造一个查询来查找给定值出现的次数就变得很简单了。

因此,类似于:

draw_values
===========

draw_id       value
-------------------
538           1
538           34
538           35
538           36
538           56
538           59
539           5
539           10
539           13
539           27
539           38
539           56
...


draw
====

draw_id       winner_id       payout
---------------------------------------
538           127740          1000000
539           839820          1500000
...

其中 draw_id 是主表的外键,其中包含每次抽奖的一次性详细信息(例如支出)。

Storing a fixed-size set of numbers as a concatenated string is madness.

Consider storing them in 6 separate columns, each of an integer type. Alternatively, store them in a table with just two columns: draw_id and value; each draw has 6 entries in this table. It then becomes trivial to construct a query to look for the number of occurrences of a given value.

So something like:

draw_values
===========

draw_id       value
-------------------
538           1
538           34
538           35
538           36
538           56
538           59
539           5
539           10
539           13
539           27
539           38
539           56
...


draw
====

draw_id       winner_id       payout
---------------------------------------
538           127740          1000000
539           839820          1500000
...

where draw_id is a foreign key into your main table, which contains the one-off details for each draw (e.g. the payout).

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