2017-08-07 02:25


  • php
  • mysql
  • join

I am trying to get the last row of table2 that I left join with table1.

Table1 column:

  1. id
  2. name
  3. created date

Table2 column:

  1. id
  2. last_login
  3. ip_address

Login data stored into table2 everytime user logged in. So I am trying to display all users from table1 which is displaying the last_login record from table2.

Here is my current query :

SELECT table1.*, table2.last_login
FROM table1
LEFT JOIN table2 ON table2.id= table1.id
ORDER BY table2.last_login desc;

With the query above I am able to get all the data from both table, where if user A logged 5 times, the query will return 5 rows, but I wanted to only showing the user details and their last_login data. If I add GROUP BY table1.id it return 1 row record for every user but the last_login data is not showing the latest record.

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享