从带有其他列的表中选择不同的值

时间:2020-05-05 17:46:52

标签: mysql sql

我有以下SQL(请注意使用distinct):

SELECT DISTINCT
    question_id AS id,
    question_id AS object_id,
    created_at AS created,
    created_at AS lastmessage,
    'T' AS object_type
FROM
    responses

...这给了我...

enter image description here

我想摆脱第一列中的重复行。我想念什么?

1 个答案:

答案 0 :(得分:1)

如果要每行一行,请使用聚合。像这样:

SELECT question_id AS id, question_id AS object_id,
       MIN(created_at) AS created,
       MAX(created_at) AS lastmessage,
       'T' AS object_type
FROM responses
GROUP BY question_id;