这是我的疑问:
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)"
我做错了什么?
答案 0 :(得分:0)
要为子查询结果提供名称(即别名),不要将别名放在子查询中,而是在子查询外部对其进行编码,如下所示:
select
...
(select count(*) from messages where unit_id = units.id) as messages
...
在不给整个子查询赋予别名的情况下,子查询结果的列名是子查询本身(这是你陈述的问题) - 最后一句)。