MIN()有条件吗? MySQL的

时间:2013-03-27 20:33:14

标签: mysql

所以我有一个包含几列的表格,我正在做GROUP BY。除了获取其他列之外,我希望在另一列MIN()时获得一列的NULL值。这是我到目前为止所做的:

表格列:

id        INT(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
table2_id INT(11) UNSIGNED NOT NULL,
iteration INT(11) UNSIGNED,
timestamp INT(11) UNSIGNED NOT NULL

SQL:

SELECT
    table2_id,
    COUNT(id) as total,
    COUNT(iteration) as completed,

    # I want the MIN(timestamp) but only when
    # iteration IS NULL
    MIN(timestamp) as next_incomplete
FROM
    table1
GROUP BY
    table2_id

1 个答案:

答案 0 :(得分:9)

将此用于MIN表达式:

MIN(CASE WHEN iteration IS NULL THEN timestamp END) as next_incomplete