如何给sql中出现的数字 [英] how to give number to occurrence in sql

查看:51
本文介绍了如何给sql中出现的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 sql 结果中给出出现次数例如我有这样的表:

I want to give number to occurrence in sql result for example I have table like this:

ID| Name | Login Date
1 |John  | 12/1/2013
2 |John  | 15/1/2013
3 |Ben   | 12/2/2013

现在我想要额外的列知道他们登录的时间:

Now I want additional column to know what times they log in:

ID| Name | Visit times
1 |John  |1
2 |John  |2 
3 |Ben   |1 

推荐答案

SELECT  t1.ID
,       t1.Name
        (
        SELECT  COUNT(t2.ID)
        FROM    tablename AS t2
        WHERE   t1.Name = t2.Name
                and t2.ID <= t1.ID
        ) AS 'Visit times'
FROM    tablename AS t1;

这篇关于如何给sql中出现的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆