我有一个包含消息的数据库。消息存储在一个表中,用户信息存储在另一个表中。在message表中,有一个author_id列,代表user表中作者的user_id,有所有message列,还有一个to_address,代表user表中“u_”+user_id的串联。有什么我可以加入这两个表的,以便它在 author_id 和 to_address 中显示用户名而不是 ID。
我试过了
SELECT username, ..., username
FROM msgs
INNER JOIN users
ON user_id=author_id AND concat("u_",user_id)=to_address;
有明显错误我尝试使用子查询,例如
SELECT
( SELECT username
FROM users
INNER JOIN msgs
ON user_id=author_id
) AS "From",
( SELECT username
FROM users
INNER JOIN msgs
ON CONCAT("u_",user_id)=to_address
) AS "To",
( SELECT timestamp(message_time) FROM msgs
) AS "Sent",
( SELECT message_subject FROM msgs
) AS "Subject",
( SELECT message_text AS "Message" FROM msgs
) AS "Message"
并得到“子查询返回超过 1 行”。有什么方法可以让我成功地做到这一点?