选择QUERY以在几小时内获得员工的帮助

时间:2014-02-17 20:43:50

标签: php mysql select range hour

我遇到了一些像QUERY这样的麻烦:

我有这张表, mt_reporteradial

enter image description here

我需要SELECT QUERY中的关注结果,确定一系列小时数(例如:11小时到15小时之间):

idmtpersonalr               11           12           13           14           15
      3                     -            -            -            X            -
      5                     -            -            -            X            X
      7                     -            -            -            X            X

其中indicador='S'表示结果中的'X'ìndicador='N'表示'-'

是否可以使用MySQL?或者我也需要用php制作它?

问候。

编辑:我有答案:

我的更新表:

enter image description here

QUERY

SELECT
    distinct(idmtpersonalr),
CASE (SELECT indicador FROM mt_reporteradial WHERE idmtpersonalr = m.idmtpersonalr AND hora='13' AND fecha='2014-02-10')
        WHEN 'S' THEN 'X'
        WHEN 'N' THEN '-'
    END as 13_hr,
    CASE (SELECT indicador FROM mt_reporteradial WHERE idmtpersonalr = m.idmtpersonalr AND hora='14' AND fecha='2014-02-10')
        WHEN 'S' THEN 'X'
        WHEN 'N' THEN '-'
    END as 14_hr,
    CASE (SELECT indicador FROM mt_reporteradial WHERE idmtpersonalr = m.idmtpersonalr AND hora='15' AND fecha='2014-02-10')
        WHEN 'S' THEN 'X'
        WHEN 'N' THEN '-'
    END as 15_hr,
    CASE (SELECT indicador FROM mt_reporteradial WHERE idmtpersonalr = m.idmtpersonalr AND hora='16' AND fecha='2014-02-10')
        WHEN 'S' THEN 'X'
        WHEN 'N' THEN '-'
    END as 16_hr
FROM
    mt_reporteradial m

2 个答案:

答案 0 :(得分:1)

像这样的东西可能会起作用:

SELECT
    distinct(idmtpersonalr),
    CASE (SELECT indicador FROM mt_reporteradial WHERE idmtpersonalr = m.idmtpersonalr AND hora='11')
        WHEN 'S' THEN 'X'
        WHEN 'N' THEN '-'
    END CASE as 11_hr,
    CASE (SELECT indicador FROM mt_reporteradial WHERE idmtpersonalr = m.idmtpersonalr AND hora='12')
        WHEN 'S' THEN 'X'
        WHEN 'N' THEN '-'
    END CASE as 12_hr,
    CASE (SELECT indicador FROM mt_reporteradial WHERE idmtpersonalr = m.idmtpersonalr AND hora='13')
        WHEN 'S' THEN 'X'
        WHEN 'N' THEN '-'
    END CASE as 13_hr,
    CASE (SELECT indicador FROM mt_reporteradial WHERE idmtpersonalr = m.idmtpersonalr AND hora='14')
        WHEN 'S' THEN 'X'
        WHEN 'N' THEN '-'
    END CASE as 14_hr,
    CASE (SELECT indicador FROM mt_reporteradial WHERE idmtpersonalr = m.idmtpersonalr AND hora='15')
        WHEN 'S' THEN 'X'
        WHEN 'N' THEN '-'
    END CASE as 15_hr
FROM
    mt_reporteradial m
WHERE
    fecha = '2014-02-10'

答案 1 :(得分:0)

你已经有了答案,但要避免使用子查询...

SELECT idmtpersonalr, 
       MAX(IF(hora=11,IF(indicador='S','X',IF(indicador='N','-','')),'')) `11`,
       MAX(IF(hora=12,IF(indicador='S','X',IF(indicador='N','-','')),'')) `12`,
       MAX(IF(hora=13,IF(indicador='S','X',IF(indicador='N','-','')),'')) `13`,
       MAX(IF(hora=14,IF(indicador='S','X',IF(indicador='N','-','')),'')) `14`,
       MAX(IF(hora=15,IF(indicador='S','X',IF(indicador='N','-','')),'')) `15`
FROM mt_reporteradial
/*WHERE fecha='2014-02-10'*/
GROUP BY idmtpersonalr
ORDER BY idmtpersonalr