[英]SQL - values from one table column to another table column
I have two tables我有两张桌子
songs (id, name, artist, artist_id)
artist (id, name)
What i am trying to do is take the id values from artist and put that value into the value of artist_id in the songs table我想要做的是从艺术家那里获取id值并将该值放入歌曲表中的artist_id的值中
The values of artist_id in songs is currently NOT NULL and has no value at the moment
ARTIST table艺术家表
id | name
1 | name1
2 | name2
etc
SONGS table歌曲表
id | name | artist | artist_id
id1 | name1 | artist1 | NOT NULL
id2 | name2 | artist2 | NOT NULL
id3 | name3 | artist3 | NOT NULL
id4 | name4 | artist4 | NOT NULL
I need it to be an UPDATE statement using subqueries and this is what I have.我需要它是使用子查询的 UPDATE 语句,这就是我所拥有的。 I do not have any errors.我没有任何错误。
The issue is that artist_id is being filled with 0's instead of the actual corresponding value of id from the artist table.问题在于, artist_id被填充为0,而不是艺术家表中id的实际对应值。
UPDATE songs
JOIN artist
ON artist.id = songs.artist_id
SET songs.artist_id = artist.id;
the subquery goes through the MySQL terminal but doesn't tranfer values over, instead just gives me 0's子查询通过 MySQL 终端但不传输值,而是只给我 0
UPDATE songs
JOIN artist
ON songs.artist_id = artist.id
SET songs.artist_id = artist.id;
I even tried to alter it a bit and it still displays a 0 instead of the corresponding value.我什至试图稍微改变它,它仍然显示 0 而不是相应的值。
The only way to relate the 2 tables is with the artist's name:关联这两个表的唯一方法是使用艺术家的名字:
UPDATE songs s
INNER JOIN artist a
ON s.artist = a.name
SET s.artist_id = a.id;
If this UPDATE
statement succeeds then you should delete the column artist
from songs
and keep only the column artist_id
.如果此UPDATE
语句成功,那么您应该从songs
删除列artist
并仅保留列artist_id
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.