急!SQL胜负查询语句

grayyy 2016-04-07 02:10:23
private void Win(object obj, EventArgs e)
{
isFinished = mineMatrix.IsFinished;
timer1.Stop();
mp.DrawFace(Face.Glasses);
mp.IsWin = true;

if (Properties.Settings.Default.soundEnable)
{
System.Media.SoundPlayer sp = new System.Media.SoundPlayer(Properties.Resources.Win);
sp.Play();
}


Properties.Settings.Default.Save();

string strConnection = "Server=(local);Database=Mine;uid=sa;pwd=netbox123*;initial catalog=Mine;Server=(local); Connect Timeout=30;";

SqlConnection objConnection = new SqlConnection(strConnection);
string sql = "insert into Count(result) values('1');";
SqlCommand Count = new SqlCommand(sql, objConnection);
objConnection.Open();
int 受影响行数 = Count.ExecuteNonQuery();
Console.WriteLine("{0}行受影响", 受影响行数);
Console.Read();
}

游戏胜利时运行这个代码,插入值1,失败则插入值0.序列号自增
现在想查询胜负率,连胜,连负,已玩局数。并将其在窗口显示出来。请问要怎么写呢?
...全文
399 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
BitCoffee 2016-04-11
  • 打赏
  • 举报
回复
根据下面的语句得出的结果,判断胜负为1时,表明当前状态时连胜,为0时连负,连负时,你自己在代码里面连续场数加个负号不就行了.
引用 12 楼 BitCoffee 的回复:

-- 当前状态,有可能连胜有可能连负
select top 1 连续场数 = max(num1) - min(num1) + 1,胜负 = result from (
   select num1 = id - 0,num2 = (select count(1) from count where id <= t.id and result = t.result),* 
   from count t 
   where t.result in (select top 1 result from count order by id desc)
) t group by t.num1 - t.num2,result order by t.num1 - t.num2 desc
BitCoffee 2016-04-11
  • 打赏
  • 举报
回复
你当前状态怎么可能是又是连胜又是连负的? 当前状态只有一种可能,根据最后一次的胜负来取,当前状态是连胜还是连负. 不知道为什么要说有两种情况.
grayyy 2016-04-11
  • 打赏
  • 举报
回复
引用 15 楼 BitCoffee 的回复:
1.case when max(连胜) is null then 0 else max(连胜) end as 最大连胜 2.你当前状态肯定只有一种,不是连胜就是连负,不可能当前状态会出现连胜连负的
哦,那如果是连负的话,用负数的连续局数来表示,胜利的话用正数来表示应该怎么写呢? 比如,现在连续3局失败,则显示-3,如果是2局胜利,则显示2
grayyy 2016-04-11
  • 打赏
  • 举报
回复
引用 13 楼 BitCoffee 的回复:

-- 胜率
select 胜率 = substring(cast((cast (a.胜场数 as decimal)/cast(b.场数 as decimal) * 100) as varchar),1,5) + '%'
from
   (select count(1) as 胜场数 from count where result = 1) a
   left join 
   (select count(1) as 场数 from count) b
   on 1 = 1
最高连胜和最高连负的null值我解决了,不过当前的连胜和连负还是不知道怎么弄,可以分开统计吗? 因为我是在游戏界面上查询的,两个都要显示。
BitCoffee 2016-04-11
  • 打赏
  • 举报
回复
1.case when max(连胜) is null then 0 else max(连胜) end as 最大连胜 2.你当前状态肯定只有一种,不是连胜就是连负,不可能当前状态会出现连胜连负的
grayyy 2016-04-09
  • 打赏
  • 举报
回复
引用 11 楼 BitCoffee 的回复:
最大连胜 = case when max(连胜) is null then 0 else max(连胜) end

提示语法错误
我自己建的表是只有ID和result两个字段的。
还有,当前连胜和连负可不可以单独查询?不然我不太清楚要在游戏中怎么显示出来。
麻烦你了
grayyy 2016-04-08
  • 打赏
  • 举报
回复
引用 4 楼 BitCoffee 的回复:
胜率,用 select count(1) from count where result = 1 除以已玩局数
百分比的研究出来了^^ 不过当前的连胜还没有,请问怎么写呢?
grayyy 2016-04-08
  • 打赏
  • 举报
回复
引用 4 楼 BitCoffee 的回复:
胜率,用 select count(1) from count where result = 1 除以已玩局数
谢谢!你给的都很有用~ 不过我还想问一下,当前的连胜怎么算呢? 还有,胜率怎么用百分比表示出来?
BitCoffee 2016-04-08
  • 打赏
  • 举报
回复

-- 胜率
select 胜率 = substring(cast((cast (a.胜场数 as decimal)/cast(b.场数 as decimal) * 100) as varchar),1,5) + '%'
from
   (select count(1) as 胜场数 from count where result = 1) a
   left join 
   (select count(1) as 场数 from count) b
   on 1 = 1
BitCoffee 2016-04-08
  • 打赏
  • 举报
回复

-- 当前状态,有可能连胜有可能连负
select top 1 连续场数 = max(num1) - min(num1) + 1,胜负 = result from (
   select num1 = id - 0,num2 = (select count(1) from count where id <= t.id and result = t.result),* 
   from count t 
   where t.result in (select top 1 result from count order by id desc)
) t group by t.num1 - t.num2,result order by t.num1 - t.num2 desc
BitCoffee 2016-04-08
  • 打赏
  • 举报
回复
最大连胜 = case when max(连胜) is null then 0 else max(连胜) end
grayyy 2016-04-08
  • 打赏
  • 举报
回复
引用 3 楼 BitCoffee 的回复:

-- 最大连胜
select 最大连胜 = max(连胜) from (
   select 连胜 = max(num1) - min(num1) + 1 from (
      select num1 = id - 0,num2 = (select count(1) from count where id <= t.id and result = 1),* from count t where result = 1
   ) t group by t.num1 - t.num2
) t
-- 最大连负
-- 将最大连胜里面的两个地方的result = 1改为result = 0即可
-- 已玩局数
select count(1) from Count
最大连胜查后怎么把NULL转换成0呢?
smthgdin_020 2016-04-07
  • 打赏
  • 举报
回复
胜率,当前连声连败局数都简单。历史最大连胜和连败数就麻烦些(代码实现和纯数据库实现2个思路)。
KJ_Wang 2016-04-07
  • 打赏
  • 举报
回复
挺简单的,看你怎么写,有兴趣发过来全部帮你搞定!
  • 打赏
  • 举报
回复
select result from table order by id desc

将你所有的记录读取出来,然后
List<int> list = new List<int>();
var 连续数 = list.TakeWhile(i => i != list[0]);//根据List[0]是0还是1来确认是连续胜,还是连续败
var 胜率 = Math.Round(list.Count(i => i == 1) * 1.0 / list.Count, 3);
BitCoffee 2016-04-07
  • 打赏
  • 举报
回复
胜率,用 select count(1) from count where result = 1 除以已玩局数
BitCoffee 2016-04-07
  • 打赏
  • 举报
回复

-- 最大连胜
select 最大连胜 = max(连胜) from (
   select 连胜 = max(num1) - min(num1) + 1 from (
      select num1 = id - 0,num2 = (select count(1) from count where id <= t.id and result = 1),* from count t where result = 1
   ) t group by t.num1 - t.num2
) t
-- 最大连负
-- 将最大连胜里面的两个地方的result = 1改为result = 0即可
-- 已玩局数
select count(1) from Count
grayyy 2016-04-07
  • 打赏
  • 举报
回复
引用 1 楼 starfd 的回复:
难道你这个表就一个Result字段?
连胜、连负又是个什么说法

ID和result两个字段。因为ID随游戏局数自增,所以不用插入。
游戏胜利插入1,游戏失败则插入0,

游戏进行多次后就这样。
连胜和连败是查询当前连续胜利局数和连续失败局数,一旦记录中断,则重新计算。
如果还可以查询历史最高的连胜和连败就更好。
胜负率是想求得胜率
  • 打赏
  • 举报
回复
难道你这个表就一个Result字段? 连胜、连负又是个什么说法

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧