简体   繁体   中英

How to split column based on row value in mysql?

I need a column from row value.

I have two table.

Table 1 : working_day Contains list of all working day date.

date
--------
2013-03-30
2013-03-29
2013-03-28

Table 2 : entry contains each employee in and out time.

id  In  Out  Date
1   9   0    2013-03-30
2   8   0    2013-03-30
3   7   0    2013-03-30
1   8   18   2013-03-29
2   9   16   2013-03-29
3   6   20   2013-03-29
4   12  15   2013-03-29

Expected Output :

ID  29-03-2013_IN  29-03-2013_Out   30-03-2013_In
1    8                    18          9
2    9                    16          8
3    6                    20          7
4    12                   15          0

Tried :

SELECT id, 
Case condition1 for 29_in,  // I don't know which condition suite here.
Case condition1 for 29_out,
Case condition1 for 30_in
FROM entry
WHERE DATE
IN (
SELECT * 
FROM (
SELECT DATE
FROM working_day
ORDER BY DATE DESC 
LIMIT 0 , 2
)a
)

You could try something like that:

select 
   e.id,
   (SELECT `in` FROM entry WHERE id = e.id AND date = '2013-03-30') as '2013-03-30_in',
   (SELECT `in` FROM entry WHERE id = e.id AND date = '2013-03-29') as '2013-03-29_in',
   (SELECT `out` FROM entry WHERE id = e.id AND date = '2013-03-29') as '2013-03-29_out'
from entry e
group by e.id;

Here is Demo

IMO你应该在应用程序而不是SQL中执行此操作

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM