I have these two tables:
USER:
id name
1 John
2 Cian
3 Una
ATTR:
id name value user_id
1 LAST_LOGGED_IN 2016-10-10 1
1 ADDED 2015-01-01 1
1 ADDED 2015-01-01 2
and wish to produce this:
user_id user_name last_logged_in added
1 John 2016-10-10 2015-01-01
2 Cian NULL 2015-01-01
3 Una NULL NULL
My attempt is:
select USER.NAME, ATTR.VALUE as LAST_LOGGED_IN
from USER
left join ATTR on USER.ID = ATTR.USER_ID
where ATTR.NAME = "LAST_LOGGED_IN"
GROUP BY USER.NAME;
but it is not correct.
select USER.NAME, IF(ATTR.NAME = 'LAST_LOGGED_IN',ATTR.VALUE,NULL) as LAST_LOGIN
from USER
right join ATTR on USER.ID = ATTR.USER_ID
GROUP BY USER.ID;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.