Mysql在组中编号的行

时间:2015-11-02 09:02:40

标签: mysql

我有桌子

    users|visits
    --------------
    user1|visit1
    user1|visit2
    user1|visit3
    user2|visit1
    user2|visit2

我希望在任何组中获得编号的行。我怎么能这样做?

users|visits|visit number
-----------------------
user1|visit1|1
user1|visit2|2
user1|visit3|3
user2|visit1|1
user2|visit2|2

2 个答案:

答案 0 :(得分:1)

尝试如下:

SELECT 
users, visits, count(*)  as `visit number`
FROM tableName group by visits,users

答案 1 :(得分:0)

根据您提供的有关您的问题,我可以想象您有类似这样的记录

users|visits
------------
user1|visit1
user1|visit2
user1|visit1
user1|visit3

你想得到

users|visits|visit number
-------------------------
user1|visit1|2
user1|visit2|1
user1|visit3|1

要做到这一点,请使用像这样的查询

SELECT 
DISTINCT a.users, 
a.visits, 
(select count(*) from tableName b where b.users = a.users and b.visits = a.visits) as `visit number`
FROM tableName  a