I've got a table user_log where I store information about user activity, for example login and logout actions. Table has four colums: id, user_id, action and timestamp
I would like to select all logout and login actions timestamps for some user_id in one result table, there would be columns like this:
user_id login_timestamp logout_timestamp.
I use this MySQL query:
SELECT
user_log.user_id,
user_log_login.login_timestamp,
user_log_logout.logout_timestamp
FROM
user_log
INNER JOIN
(SELECT
user_id, timestamp as login_timestamp
FROM
user_log
WHERE
action = 'login successful') as user_log_login
ON
user_log.user_id = user_log_login.user_id
INNER JOIN
(SELECT
user_id, timestamp as logout_timestamp
FROM
user_log
WHERE
action = 'logout successful') as user_log_logout
ON
user_log.user_id = user_log_logout.user_id
WHERE
user_log.user_id = 4
But the result contains duplicates of login_timestamp.
Have a look:

logout_timestampforuser_id = 4.