1

Using drush, how can you see the last user that logged in? What about the last 5, 10, 20?

3 Answers 3

2
drush sqlq "SELECT name FROM users ORDER BY login DESC LIMIT 10"

Tweak LIMIT to change the number returned.

0
2

You could use drush sql-query to do this:

drush sql-query "SELECT * FROM users ORDER BY login DESC LIMIT 10"

The above will display the 10 latest logins (or less).

If you only want some name you can change the * to name and you can also change the LIMIT 10 to the number of results you would like to display.

For example this would return the name and uid for the last 5 users:

drush sql-query "SELECT uid, name FROM users ORDER BY login DESC LIMIT 5"

Here is the structure for the users table:

+------------------+------------------+------+-----+---------+-------+
| Field            | Type             | Null | Key | Default | Extra |
+------------------+------------------+------+-----+---------+-------+
| uid              | int(10) unsigned | NO   | PRI | 0       |       |
| name             | varchar(60)      | NO   | UNI |         |       |
| pass             | varchar(128)     | NO   |     |         |       |
| mail             | varchar(254)     | YES  | MUL |         |       |
| theme            | varchar(255)     | NO   |     |         |       |
| signature        | varchar(255)     | NO   |     |         |       |
| signature_format | varchar(255)     | YES  |     | NULL    |       |
| created          | int(11)          | NO   | MUL | 0       |       |
| access           | int(11)          | NO   | MUL | 0       |       |
| login            | int(11)          | NO   |     | 0       |       |
| status           | tinyint(4)       | NO   |     | 0       |       |
| timezone         | varchar(32)      | YES  |     | NULL    |       |
| language         | varchar(12)      | NO   |     |         |       |
| picture          | int(11)          | NO   | MUL | 0       |       |
| init             | varchar(254)     | YES  |     |         |       |
| data             | longblob         | YES  |     | NULL    |       |
+------------------+------------------+------+-----+---------+-------+

Here is an explaination of some of the columns:

  • uid - Unique user id
  • name - Unique user name
  • mail - Users e-mail address
  • created - Timestamp for when user was created
  • access - Timestamp for previous time user accessed the site
  • login - Timestamp for user?s last login
  • status - Whether the user is active(1) or blocked(0)
3
  • darn just beat me to it! =P... Came up with this: drush sql-query 'select uid, access, login, name from users order by access desc limit 3 \G' Commented Aug 6, 2015 at 19:53
  • what is the difference from Access and Login table? Commented Aug 6, 2015 at 19:54
  • @duckx I assume you're asking about the access and login fields rather than tables. The access field is a timestamp for the last time the user accessed the site (basically, the last time that authenticated user made a request to the server of any kind). The login field is just what it sounds like: a timestamp of the last time a user logged into the site.
    – Aaron
    Commented Aug 6, 2015 at 19:59
2

The same functionality for Drupal 8.x+. The users' table is "users_field_data".

drush sqlq "SELECT uid,name,mail FROM users_field_data ORDER BY login DESC LIMIT 10"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.