0

我在用着

SELECT m.id, r.username AS `from`, s.username AS `to`, m.message
FROM msgs m
INNER JOIN usrs r ON r.user_id = m.from
INNER JOIN usrs s ON s.user_id = m.to;

在消息的'from'和'to'列中用用户名替换用户ID,然后将其输出到带有mysql usrmsgs < script.sql > output.txt的文件

我想将 msgs 表本身的“from”和“to”中的用户 ID 替换为来自 usrs 的相应用户名。最好的方法是什么?

4

1 回答 1

0
alter table msgs
    add column from_name varchar(<size>),
    add column to_name varchar(<size>);

update msgs m 
join usrs r ON r.user_id = m.from
join usrs s ON s.user_id = m.to
SET m.from_name = r.username, m.to_name = s.username;

alter table msgs drop column from, drop column to;
于 2012-11-25T09:26:07.187 回答