SELECT
M.FromUserID,
UF.Fname + ' ' + UF.Sname As FullName,
UF.URL as Username,
Subject,
Body,
M.Ctime,
M.ViewedDate,
M.MessageID,
M.Starred,
M.Deleted,
M.ToUserID,
(SELECT TOP 1 Fname + ' ' + Sname as FullName FROM user_basics UB WHERE UB.UserID = M.ToUserID) as ToName,
(SELECT TOP 1 URL FROM user_basics UB WHERE UB.UserID = M.ToUserID) as ToUserName
FROM Messages M
LEFT JOIN User_Basics UF ON UF.userID = M.FromUserID
WHERE M.ToUserID = 1433 AND Deleted IS NULL ORDER BY M.Ctime DESC
最初にユーザー名でグループ化された最新のメッセージでメッセージ履歴を表示したい...
group by メソッドを使用すると、他のすべての列を追加する必要があることがわかります...
Column 'Messages.FromUserID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
もっと良い方法があるはずですか?