5

所以我有一个有几列的表,我正在做一个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
4

1 回答 1

13

将其用于MIN表达式:

MIN(CASE WHEN iteration IS NULL THEN timestamp END) as next_incomplete
于 2013-03-27T20:35:58.513 回答