MySQL-从具有引用ID的行中查找值
下表记录了测量值(空、1/4、半、3/4、满)及其日期MySQL-从具有引用ID的行中查找值,mysql,Mysql,下表记录了测量值(空、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 ha
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,你在预期的结果中显示了这一点。类似的东西应该满足你的要求:
select
`ID`,
coalesce(
if (`m1` = 'half', `Time1`, null),
if (`m2` = 'half', `time2`, null),
if (`m3` = 'half', `Time3`, null),
if (`m4` = 'half', `time4`, null)
) time_half
from _data
谢谢你,我最后用了一个案例来说明每一个有效的测量。FWIW,在我看来,所谓的“纵向表”是一个非常糟糕的设计。“横截面”表格更好,但最好是存储小数。它是一种以这种方式记录信息的软件,测量值作为文本字符串,日期作为时间戳(我只包括了日期,以适应表格上的所有内容),此外,该软件的测量值可能超过时间。理想情况下,我想要一个简单的方法来制作“横截面”表格。最后,我使用了多个“case”语句,只查看了最后5个测量值,例如计算从一半到全部的平均时间。