我有下表记录测量值(空、1/4、一半、3/4、满)及其日期。
ID m1 Time1 m2 time2 m3 Time3 m4 time4
1 half 01/02/2018 3/4 06/06/2018 full 07/06/2018
2 3/4 18/01/2018 full 30/06/2018
3 full 26/04/2018
4 1/4 01/02/2018 half 29/06/2018 3/4 01/08/2018 full 03/08/2018
是否可以在测量一半时查询日期以获得以下输出:
ID time_full
1 01/02/2018
2 NULL
3 NULL
4 29/06/2018
或者我们可以很容易地将这个纵向表转换成这样的横截面表吗
ID m time
1 half 01/02/2018
1 3/4 06/06/2018
1 full 07/06/2018
2 3/4 18/01/2018
2 full 30/06/2018
3 full 26/04/2018
(...)
谢谢
最佳答案
select id,
case m when 'half' then m else null end m,
case m when 'half' then time1 else null end time_full
from table
但是您的示例中,4 是 1/4,并且您在预期结果中显示了它。
关于MySQL - 从具有引用 ID 的行中查找值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52321472/