-4

这是我的查询:

SELECT  messages.*, 
        units.*, 
        MAX(messages.created_at) AS 'conversation_date', 
        (select count(*) as messages from messages where unit_id = units.id), 
        WEEKOFYEAR(MAX(messages.created_at)) as 'woy', 
        YEAR(MAX(messages.created_at)) as 'year',
        classifications.description AS 'classification'
FROM    messages 
JOIN    units ON units.id = messages.unit_id
JOIN    classifications ON units.classification_id = classifications.id
WHERE   from_id <> units.creator_id GROUP BY messages.unit_id, from_id, to_id

我正在尝试命名第 4 列"messages",但名称按字面意思返回"(select count(*) as messages from messages where unit_id = units.id)"

我究竟做错了什么?

4

1 回答 1

0

要为子查询结果命名(即别名),您不要将别名放在子查询中,而是在子查询之外对其进行编码,如下所示:

select
...
(select count(*) from messages where unit_id = units.id) as messages
...

在不为整个子查询提供别名的情况下,为子查询结果提供的列名是子查询本身(这是您陈述的问题) - 最后一句)。

于 2014-08-06T20:42:20.797 回答