我需要行值中的一列。

我有两个桌子。

表1:working_day包含所有工作日日期的列表。

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


表2:条目包含每个员工的进出时间。

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


预期产量:

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


尝试过:

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
)

最佳答案

您可以尝试这样的事情:

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;


这是Demo

关于mysql - 如何在mysql中基于行值拆分列?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15715390/

10-10 02:54