如何获得最大的null& sql中的数字为null?

时间:2017-09-01 07:17:43

标签: php mysql sql xampp

考虑一个示例SQL表,其中有两列和值插入为

INSERT INTO `over_all` (`team_number`, `day_1`) VALUES
('J091', '01:00:00'),
('J091', '01:02:00');

INSERT INTO `over_all` (`team_number`, `day_1`) VALUES
('J092', 'null'),
('J092', '02:02:00');

现在我使用

SELECT `over_all`.`team_number`,
        max(`over_all`.`day_1`) as 'maximum'
FROM `over_all`
GROUP BY `over_all`.`team_number`
ORDER BY 'maximum'

问题:

  

我希望这能将J092的最大值返回为 null

使用数据库

  

服务器版本:10.1.25-MariaDB - mariadb.org二进制分发

1 个答案:

答案 0 :(得分:2)

您可以使用CASE WHEN ... THEN ... ELSE ... END

来完成此操作
SELECT team_number,
    case when max(day_1 is null) = 0 then max(day_1) else null end as maximum
FROM over_all
GROUP BY team_number
ORDER BY maximum

在此处试试:http://sqlfiddle.com/#!9/101143/2/0