MySQL UPDATE错误列不存在

时间:2017-06-21 17:20:18

标签: mysql

我正在尝试使用另一个表中的内容更新一个表,其中一个表中的player_id与另一个表中的id匹配。

我的查询是:

UPDATE match_request
SET first_name = users.first_name, last_name = users.last_name, phone = users.phone, email = users.phone
WHERE player_id = users.id

但我不能为我的生活弄清楚为什么这不起作用。错误是users.id是一个未知的列,但肯定存在并且都是小写的。

3 个答案:

答案 0 :(得分:1)

您需要加入users表格。

UPDATE match_request AS m
JOIN users AS u ON m.player_id = u.id
SET m.first_name = u.first_name, m.last_name = u.last_name, m.phone = u.phone, m.email = u.phone

答案 1 :(得分:0)

您需要指定INNER JOIN

UPDATE match_request
INNER JOIN users ON match_request.player_id = users.id
SET match_request.first_name = users.first_name,
    match_request..last_name = users.last_name,
    match_request..phone = users.phone,
    match_request..email = users.phone

答案 2 :(得分:0)

这里,这种sql查询需要标量值,字符,字符串或连接表值,这样的例子给出为

UPDATE table_name 
SET column_name1 = 'XXXX', column_name2 = 5 
WHERE column_named_id = 3; 

但是在这里你使用where子句和另一个表列名users.id

您必须首先加入表,然后使用where子句作为whatevet用户表;

UPDATE match_request AS t1
INNER JOIN users ON t1.primary_key_id = users.foreign_key_id
SET t1.first_name = users.first_name, t1.last_name = users.last_name, t1.phone = users.phone, t1.email = users.phone
WHERE  t1.player_id = users.id

此处您必须只填写inner join声明