我把一些引用放在MySql表中是错误的。我想一个例子会告诉你我的问题(我真的希望如此)。下面是我的表结构的一个示例:

tableMain
id (int)
trackid(varchar)
data(text)

tableArtist
idArtist(int)
ref(varchar)
artist(varchar)

tableEvent
idEvent(int)
ref(varchar)
event(varchar)

以下是数据:
tableMain
1       abc    primo
2       def    secondo
3       ghi    terzo

tableArtist
1       abc    artist2
2       abc    artist4
3       ghi    artist5
4       def    artist1
5       ghi    artist3

tableEvent
1       def    event1
2       abc    event5
3       222    event3
4       ghi    event2
5       abc    event4

我想更改tableEvent和tableArtist上ref的值,替换为tableMain中corespondent I d的值:
tableMain
1       abc     primo
2       def     secondo
3       ghi     terzo

tableArtist
1       1     artist2
2       1     artist4
3       2     artist5
4       2     artist1
5       3     artist3

tableEvent
1       2     event1
2       1     event5
3       2     event3
4       3     event2
5       1     event4

是在MySql上还是我需要一种脚本(比如PHP)来完成?

最佳答案

UPDATE tableArtist, tableMain
    SET tableArtist.ref = tableMain.id
    WHERE tableArtist.ref = tableMain.trackid

UPDATE tableEvent, tableMain
    SET tableEvent.ref = tableMain.id
    WHERE tableEvent.ref = tableMain.trackid

编辑:为了解决@colinmarc的问题,在更新数据之后,您可以:
ALTER TABLE tableArtist MODIFY ref INT;
ALTER TABLE tableEvent MODIFY ref INT;

09-25 18:25
查看更多