Skip to main content
added 86 characters in body
Source Link
McNets
  • 23.9k
  • 11
  • 50
  • 89

In this case I'd suggest to use a subquery to count rows using a variable.

select   user_id, name, group_concat(login_date)
from     (select user_id, login_date, name
          from (select user_id, login_date, name,
                       if(@last_user = user_id, @rn := @rn + 1, @rn := 0) rn,
                       @last_user := user_id 
                from (select @last_user := 0, @rn := 0) a,
                     (select login_trackusers.user_id, login_date, users.name
                      from login_trackusers
                      left join userslogin_track
                      on users.user_id = login_track.user_id
                      order by users.user_id, login_date desc) b
               ) c
          where rn <= 1
         ) d
group by user_id; 


| user_id | name     | group_concat(login_date) |
|---------|----------|--------------------------|
| 1       | John     | 2017-06-27,2017-06-23    |
| 2       | Mike     | 2017-06-23,2017-06-21    |
| 3       | Anderson | NULL                     |

rextester herehere

In this case I'd suggest to use a subquery to count rows using a variable.

select   user_id, name, group_concat(login_date)
from     (select user_id, login_date, name
          from (select user_id, login_date, name,
                       if(@last_user = user_id, @rn := @rn + 1, @rn := 0) rn,
                       @last_user := user_id 
                from (select @last_user := 0, @rn := 0) a,
                     (select login_track.user_id, login_date, name
                      from login_track
                      join users
                      on users.user_id = login_track.user_id
                      order by user_id, login_date desc) b
               ) c
          where rn <= 1
         ) d
group by user_id;

| user_id | name | group_concat(login_date) |
|---------|------|--------------------------|
| 1       | John | 2017-06-27,2017-06-23    |
| 2       | Mike | 2017-06-23,2017-06-21    |

rextester here

In this case I'd suggest to use a subquery to count rows using a variable.

select   user_id, name, group_concat(login_date)
from     (select user_id, login_date, name
          from (select user_id, login_date, name,
                       if(@last_user = user_id, @rn := @rn + 1, @rn := 0) rn,
                       @last_user := user_id 
                from (select @last_user := 0, @rn := 0) a,
                     (select users.user_id, login_date, users.name
                      from users
                      left join login_track
                      on users.user_id = login_track.user_id
                      order by users.user_id, login_date desc) b
               ) c
          where rn <= 1
         ) d
group by user_id; 


| user_id | name     | group_concat(login_date) |
|---------|----------|--------------------------|
| 1       | John     | 2017-06-27,2017-06-23    |
| 2       | Mike     | 2017-06-23,2017-06-21    |
| 3       | Anderson | NULL                     |

rextester here

added 167 characters in body
Source Link
McNets
  • 23.9k
  • 11
  • 50
  • 89

In this case I'd suggest to use a subquery to count rows using a variable.

select   user_id, name, group_concat(login_date)
from     (select user_id, login_date, name
          from (select user_id, login_date, name,
                       if(@last_user = user_id, @rn := @rn + 1, @rn := 0) rn,
                       @last_user := user_id 
                from (select @last_user := 0, @rn := 0) a,
                     (select login_track.user_id, login_date, name
                      from login_track
                      join users
                      on users.user_id = login_track.user_id
                      order by user_id, login_date desc) b
               ) c
          where rn <= 1
         ) d
group by user_id;

| user_id | name | group_concat(login_date) |
|---------|------|--------------------------|
| 1       | John | 2017-06-27,2017-06-23    |
| 2       | Mike | 2017-06-23,2017-06-21    |

rextester herehere

In this case I'd suggest to use a subquery to count rows using a variable.

select   user_id, group_concat(login_date)
from     (select user_id, login_date
          from (select user_id, login_date,
                       if(@last_user = user_id, @rn := @rn + 1, @rn := 0) rn,
                       @last_user := user_id 
                from (select @last_user := 0, @rn := 0) a,
                     (select user_id, login_date 
                      from login_track 
                      order by user_id, login_date desc) b
               ) c
          where rn <= 1
         ) d
group by user_id;

| user_id | group_concat(login_date) |
|---------|--------------------------|
| 1       | 2017-06-27,2017-06-23    |
| 2       | 2017-06-23,2017-06-21    |

rextester here

In this case I'd suggest to use a subquery to count rows using a variable.

select   user_id, name, group_concat(login_date)
from     (select user_id, login_date, name
          from (select user_id, login_date, name,
                       if(@last_user = user_id, @rn := @rn + 1, @rn := 0) rn,
                       @last_user := user_id 
                from (select @last_user := 0, @rn := 0) a,
                     (select login_track.user_id, login_date, name
                      from login_track
                      join users
                      on users.user_id = login_track.user_id
                      order by user_id, login_date desc) b
               ) c
          where rn <= 1
         ) d
group by user_id;

| user_id | name | group_concat(login_date) |
|---------|------|--------------------------|
| 1       | John | 2017-06-27,2017-06-23    |
| 2       | Mike | 2017-06-23,2017-06-21    |

rextester here

added 428 characters in body
Source Link
McNets
  • 23.9k
  • 11
  • 50
  • 89

In this case I'd suggest to use a subquery to count rows using a variable.

select   user_id, group_concat(login_date)
from     (select user_id, login_date
          from (select user_id, login_date,
                       if(@last_user = user_id, @rn := @rn + 1, @rn := 0) rn,
                       @last_user := user_id 
                from (select @last_user := 0, @rn := 0) a,
                     (select user_id, login_date 
                      from login_track 
                      order by user_id, login_date desc) b
               ) c
          where rn <= 1
         ) d
group by user_id;

| user_id | group_concat(login_date) |
|---------|--------------------------|
| 1       | 2017-06-27,2017-06-23    |
| 2       | 2017-06-23,2017-06-21    |

rextester here

In this case I'd suggest to use a subquery to count rows using a variable.

select   user_id, group_concat(login_date)
from     (select user_id, login_date
          from (select user_id, login_date,
                       if(@last_user = user_id, @rn := @rn + 1, @rn := 0) rn,
                       @last_user := user_id 
                from (select @last_user := 0, @rn := 0) a,
                     (select user_id, login_date from login_track) b
               ) c
          where rn <= 1
         ) d
group by user_id;

In this case I'd suggest to use a subquery to count rows using a variable.

select   user_id, group_concat(login_date)
from     (select user_id, login_date
          from (select user_id, login_date,
                       if(@last_user = user_id, @rn := @rn + 1, @rn := 0) rn,
                       @last_user := user_id 
                from (select @last_user := 0, @rn := 0) a,
                     (select user_id, login_date 
                      from login_track 
                      order by user_id, login_date desc) b
               ) c
          where rn <= 1
         ) d
group by user_id;

| user_id | group_concat(login_date) |
|---------|--------------------------|
| 1       | 2017-06-27,2017-06-23    |
| 2       | 2017-06-23,2017-06-21    |

rextester here

Post Undeleted by McNets
added 428 characters in body
Source Link
McNets
  • 23.9k
  • 11
  • 50
  • 89
Loading
Post Deleted by McNets
Source Link
McNets
  • 23.9k
  • 11
  • 50
  • 89
Loading