简体   繁体   English

在SQL Server上以逗号分隔的数字字符串中找到匹配项的方法

[英]Method to find hits in comma-separated number string on SQL Server

I have a Windows forms (c#) application and a table in SQL Server that has two columns like this: 我有一个Windows窗体(c#)应用程序和SQL Server中的一个表,该表具有两列,如下所示:

ticket (int) | numbers (string)
12345        | '01, 02, 04, 05, 09, 10, 23'

This table may have like 100.000 rows or more. 该表可能有100.000行或更多。

Where I have to do is to found the amount of hits giving an array of numbers like a lottery. 我要做的是找到像彩票一样的命中数量。 I have 12 hits, 11 hits and 9 hits for example and for each raffled number I have to perform the search of what win the 12 hits, 11 hits or 9 hits. 例如,我有12个命中,11个命中和9个命中,对于每个抽奖号码,我都必须搜索12个命中,11个命中或9个命中什么。

So, how is the best way to get this approach? 那么,获得这种方法的最佳方法是什么? I need the best performance. 我需要最好的表现。

For now I have this code: 现在,我有以下代码:

string sentSQL = " SELECT ticket, numbers FROM tableA";

/* CODE TO PERFORM THE CONNECTION */
/*...*/

DbDataReader reader = connection.ExecuteReader();

int hits12, hits11, hits9 = 0;
int count;

while (reader.Read())
{
                count = 0;
                string numbers = reader["numbers"].ToString();
                string ticketNumber = reader["ticket"].ToString();
                int maxJ = balls.Count; //balls is the ArrayList with the numbers currently extracted in the raffle
                for (int j = 0; j < maxJ; j++)
                {
                    if (numbers.Contains(balls[j].ToString()))
                    {
                        count++;
                    }
                }
                switch (count)
                        {
                            case 12:
                                hits12++;
                                break;
                            case 11:
                                hits11++;
                                break;
                            case 9:
                                hits9++;
                                break;
                        }
}

This is working but maybe there is a better method to make it possible. 这是可行的,但也许有更好的方法可以实现。

I'm using SQL Server 2012, maybe is there a function that help me? 我正在使用SQL Server 2012,也许有功能对我有帮助吗?

Edit: Can i perform in the sql query a SUM of the CHARINDEX of each number to get the amount of hits inside the sql query? 编辑:我可以在SQL查询执行SUM中的CHARINDEX每个号码来获得SQL查询中点击量?

You currently have a totally tacky solution. 您目前有一个完全俗气的解决方案。

create table ticket (
    ticketId int not null -- PK
)
create table TicketNumbers *
    ticketId int not null,
    numberSelected int not null
)

TicketNumbers has an FK to Ticket, and a PK of TicketNumber + numberSelected. 票证编号具有票证的FK,以及票证编号+已选定号码的PK。

select t.ticketId, count(*) CorrectNumbers
from ticket t
inner join TicketNumbers tn on tn.ticketId = t.TicketId
where tn.numberSelected in (9, 11, 12, 15) -- list all winning numbers
group by t.ticketId
order by count(*) desc

Cheers - 干杯-

One simple way to improve this is to update your select statement to get only records with numbers greater than your first ball number and less that your last ball number + 1 ... 一种改善此问题的简单方法是更新您的select语句,以仅获取数字大于您的第一个球号且小于最后一个球号+ 1的记录。

Example (probably not correct SQL): 示例(可能是不正确的SQL):

SELECT ticket, numbers FROM tableA where '10' < numbers and '43' > numbers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM