Place records on one line only

0

Problem: The user does several logins and several logouts, he wanted to log in and log out on the same line next to the user name, with the login date and the logout date, on the same line. All records in one line only.

    
asked by anonymous 26.10.2017 / 20:13

2 answers

0

I would use log manipulation for this type of task I believe sql would not be the best way to know when the user logged in or out.

I recommend you take a look at .txt file manipulation

or change the database table as follows

| id | User | last_login | last_logout |

  

CREATE TABLE table_name (

     

id INT (11) NOT NULL AUTO_INCREMENT PRIMARY KEY,

     

user VARCHAR (60),

     

ultimo_login VARCHAR (30),

     

ultimo_logout VARCHAR (30)

     

);

I put the dates of the table as a string because I do not know what language you're going to work on.

    
26.10.2017 / 20:26
0

You can make this query using a subquery.

See it working: SQLFiddle

SELECT 
  s1.nomusuario [Usuario]
  , 'Login/Logout'
  , s1.data [Login]
  , (
    SELECT MIN(data)
    FROM sistema [s2] 
    WHERE s2.nomusuario = s1.nomusuario
    AND s2.operacao = 'logout'
    AND s2.data > s1.data
  )
FROM sistema [s1]
WHERE s1.operacao = 'login'
    
26.10.2017 / 20:31