我希望通过加入table_1中的userID来获取table_1中的所有表记录和table_2中的Username
“UserID保存在table_1中,列名为msg_from”
以下查询仅按用户提取一条记录。请帮我收到所有消息。
SELECT t1.id, t1.subject, t1.msg_from, t1.msg_date, t1.msg_status, t2.username
FROM table_1 AS t1, table_2 AS t2
WHERE t2.id = t1.msg_from
答案 0 :(得分:1)
您应该能够通过类似于此的LEFT JOIN来实现这一目标:
"SELECT t1.id, t1.subject, t1.msg_from, t1.msg_date, t1.msg_status, t2.username
FROM table_1 AS t1
LEFT JOIN table_2 AS t2 ON t1.msg_from = t2.UserID"
从您发布的内容中确定t2上用户ID的字段名称是什么,所以我使用" t2.UserID",但您可以轻松地将其替换为真实字段名称。如果您最终得到重复记录,则可以添加GROUP BY。
答案 1 :(得分:0)
SELECT
t1.id,
t1.subject,
t1.msg_from,
t1.msg_date,
t1.msg_status,
t2.username
FROM table_1 AS t1
LEFT JOIN table_2 AS t2
ON t2.id = t1.msg_from
如果您的列名称为UserID
,那么您可以将t2.UserID = t1.msg_from
放在查询的最后一位。