求语句,字符串比较!
本帖最后由 zenter 于 2013-08-27 23:47:22 编辑 表名:tablename --用户买彩票的表
列名:h1,h2,h3,h4,h5,h6,h7 --用户买彩票的号码表 h1-h6为红球 h7为蓝球
参数:05,06,08,09,12,13,05 --开出的号码
问:怎么通过一条语句查询中出1-5等奖的用户列表(每次查询只输出一个级别的奖)
比如 查询语句,参数1=兑奖号码 参数2=要查询的兑奖级别1-5 返回数据集
1-5等奖的规则
1等奖 = 6个红球+1个蓝球
2等奖= 6个红球
3等奖= 5个红球+1个蓝球
4等奖= 5个红球
= 4个红球+1个蓝球
5等奖=4个红球
=3个红球+1个蓝球
6等奖=2个红球+1个蓝球
= 1个红球+1个蓝球
= 1个蓝球
[解决办法]
数据库方面做查询购买号码和开奖号码就行
用下面的改改就好
string[] Redkaijiang = { "01", "02", "03", "04", "05", "06" };
string Bluekaijiang = "07";
int BlueCount = 0;
int RedCount = 0;
string[] Redgoumai = { "01", "02", "03", "04", "05", "06" };
string Bluegoumai = "07";
for (int i = 0; i < Redkaijiang.Length; i++)
{
for (int j = 0; j < Redgoumai.Length; j++)
{
if (Redkaijiang[i].Equals(Redgoumai[j]))
{
RedCount = RedCount + 1;
}
}
}
if (Bluegoumai == Bluekaijiang)
{
BlueCount =BlueCount+ 1;
}
if (RedCount == 6 && BlueCount == 1)
{
Response.Write("一等奖");
}
else if (RedCount == 6)
{
Response.Write("二等奖");
}
else if (RedCount == 5 && BlueCount == 1)
{
Response.Write("三等奖");
}
else if ((RedCount == 5)
[解决办法]
(RedCount == 4 && BlueCount == 1))
{
Response.Write("四等奖");
}
else if ((RedCount == 4)
[解决办法]
(RedCount == 3 && BlueCount == 1))
{
Response.Write("五等奖");
}
else if ((RedCount == 2&&BlueCount==1)
[解决办法]
(RedCount == 1 && BlueCount == 1)
[解决办法]
(BlueCount==1))
{
Response.Write("六等奖");
}
IF object_id('tempdb..#tb') IS NOT NULL DROP TABLE #tb
go
CREATE TABLE #tb(h1 char(2),h2 char(2),h3 char(2),h4 char(2),h5 char(2),h6 char(2),h7 CHAR(2))
INSERT #tb
SELECT '05', '06', '08', '09', '12', '13', '05' union ALL
SELECT '05', '08', '08', '09', '12', '13', '05' union ALL
SELECT '05', '08', '07', '09', '12', '13', '05' union ALL
SELECT '05', '06', '06', '09', '12', '13', '05' union ALL
SELECT '05', '06', '08', '09', '12', '13', '05' union ALL
SELECT '00', '00', '00', '00', '00', '00', '05'
GO
--sql:
DECLARE @兑奖号码 VARCHAR(100), @兑奖级别 INT
SELECT @兑奖号码 = '05,06,08,09,12,13,05', @兑奖级别 = 1
SELECT h1,h2,h3,h4,h5,h6,h7
FROM
(
SELECT
兑奖级别 =
CASE
WHEN (红球数量=6 AND 蓝球数量=1) THEN 1
WHEN (红球数量=6) THEN 2
WHEN (红球数量=5 AND 蓝球数量=1) THEN 3
WHEN ((红球数量=5) OR (红球数量=4 AND 蓝球数量=1)) THEN 4
WHEN ((红球数量=4) OR (红球数量=3 AND 蓝球数量=1)) THEN 5
WHEN ((红球数量=2 AND 蓝球数量=1) OR (红球数量=1 AND 蓝球数量=1) OR (蓝球数量=1)) THEN 6--这儿可以简化成:蓝球数量=1
ELSE 0
END,
h1,h2,h3,h4,h5,h6,h7
FROM
(
SELECT
红球数量 = (CASE WHEN h1 = SUBSTRING(@兑奖号码, 1, 2) THEN 1 ELSE 0 END)
+(CASE WHEN h2 = SUBSTRING(@兑奖号码, 4, 2) THEN 1 ELSE 0 END)
+(CASE WHEN h3 = SUBSTRING(@兑奖号码, 7, 2) THEN 1 ELSE 0 END)
+(CASE WHEN h4 = SUBSTRING(@兑奖号码, 10, 2) THEN 1 ELSE 0 END)
+(CASE WHEN h5 = SUBSTRING(@兑奖号码, 13, 2) THEN 1 ELSE 0 END)
+(CASE WHEN h6 = SUBSTRING(@兑奖号码, 16, 2) THEN 1 ELSE 0 END),
蓝球数量 = (CASE WHEN h7 = SUBSTRING(@兑奖号码, 19, 2) THEN 1 ELSE 0 END),
h1,h2,h3,h4,h5,h6,h7
FROM #tb
) t
) m
WHERE 兑奖级别 = @兑奖级别
/*
h1h2h3h4h5h6h7
05060809121305
05060809121305
*/