Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to group by column name and ensure the query retrieves the last update

Tags:

php

mysql

view

I am currently working with MySQL creating a view that would return the following:

NAME | EMAIL | LAST_SEEN

abby | [email protected] | 2015-10-31 14:36:26
abby | [email protected] | 2015-11-28 13:30:37

I then apply the GROUP BY name to the select query and it returns the following

 NAME | EMAIL | LAST_SEEN

 abby | [email protected] | 2015-10-31 14:36:26

I want to know how can I fix this query so that it returns the following:

NAME | EMAIL | LAST_SEEN

abby | [email protected] | 2015-11-28 13:30:37

the actual code is as follows:

CREATE VIEW v_user_last_seen
AS
SELECT concat_ws(' ', u.first_name, u.middle_name, u.last_name) AS user_name
    ,c.email
    ,l.in_when AS last_seen
FROM user AS u
INNER JOIN check_here_first AS c ON c.email = u.email
INNER JOIN log AS l ON l.u_id = c.username
GROUP BY user_name
ORDER BY user_name ASC
like image 465
F.Hall Avatar asked Dec 12 '25 21:12

F.Hall


1 Answers

simply use max(last_seen)

 select name, email, max(last_seen) 
 from  yourtable,
 group by name, email;
like image 136
ScaisEdge Avatar answered Dec 15 '25 10:12

ScaisEdge