Question

i have mysql data (table) from attendance program like this :

+--------+--------------------+-----------+
| userid | checktime          | checktype | 
+-----------------------------+-----------+
|      1 | 26/04/2013 8:05:17 | I         |
|      2 | 26/04/2013 8:05:17 | I         |
|      2 | 26/04/2013 17:28:47| O         |
|      3 | 26/04/2013 17:32:24| O         |
+-----------------------------------------+

and i want preview like this :

+--------+--------------------+---------------------+
| userid | Login time         | Logout time         | 
+-----------------------------+---------------------+
|      1 | 26/04/2013 8:05:17 | Null                |
|      2 | 26/04/2013 8:05:17 | 26/04/2013 17:28:47 |
|      3 | Null               | 26/04/2013 17:32:24 |
+---------------------------------------------------+

how should I write a query to preview this output?? any suggestions will be helpful for me.please help...

Was it helpful?

Solution

With the information you provided this query should do it:

SELECT
  userid,
  max(if(checktype = 'I', checktime, NULL)) loginTime,
  max(if(checktype = 'O', checktime, NULL)) logoutTime
FROM t
GROUP BY userid

Output:

| USERID |          LOGINTIME |          LOGOUTTIME |
|--------|--------------------|---------------------|
|      1 | 26/04/2013 8:05:17 |              (null) |
|      2 | 26/04/2013 8:05:17 | 26/04/2013 17:28:47 |
|      3 |             (null) | 26/04/2013 17:32:24 |

Fiddle here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top