Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
147 views
Welcome To Ask or Share your Answers For Others

1 Answer

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...