Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 更新连接另一个表的表_Mysql - Fatal编程技术网

Mysql 更新连接另一个表的表

Mysql 更新连接另一个表的表,mysql,Mysql,更新一个表并连接一个或多个表 UPDATE t1 SET t1.col1 =1 FROM table1 t1 JOIN table2 t2 ON t1.ID=t2.ID WHERE t1.Name='Test' AND t2.Age=25; 我得到这个错误,你的SQL语法有一个错误;查看与您的MySQL服务器版本对应的手册,了解使用“FROM table1 t1 JOIN table2 t2”的正确语法 有什么想法吗 谢谢。语句中不应该有FROM子句,而SET子句应该跟在全套表格引用之后

更新一个表并连接一个或多个表

UPDATE t1 SET  t1.col1 =1 FROM table1 t1 JOIN  table2 t2 
ON t1.ID=t2.ID
WHERE t1.Name='Test' AND t2.Age=25;
我得到这个错误,你的SQL语法有一个错误;查看与您的MySQL服务器版本对应的手册,了解使用“FROM table1 t1 JOIN table2 t2”的正确语法

有什么想法吗


谢谢。

语句中不应该有
FROM
子句,而
SET
子句应该跟在全套表格引用之后:

UPDATE  table1 t1 
JOIN    table2 t2 ON t1.ID = t2.ID
SET     t1.col1 = 1
WHERE   t1.Name = 'Test' AND t2.Age = 25;
测试用例:

CREATE TABLE table1 (id int, col1 int, name varchar(20));
CREATE TABLE table2 (id int, age int);

INSERT INTO table1 VALUES (1, 0, 'Test');
INSERT INTO table1 VALUES (2, 0, 'Test');
INSERT INTO table1 VALUES (3, 0, 'No Test');

INSERT INTO table2 VALUES (1, 20);
INSERT INTO table2 VALUES (2, 25);
INSERT INTO table2 VALUES (3, 25);
结果:

SELECT * FROM table1;
+------+------+---------+
| id   | col1 | name    |
+------+------+---------+
|    1 |    0 | Test    |
|    2 |    1 | Test    |
|    3 |    0 | No Test |
+------+------+---------+
3 rows in set (0.00 sec)

我在更新加入中遇到问题。在表1中,我保存了username而不是userid。 用户名的数据类型是varchar(10)。当名称超过10个字符时,名称仅以10个字符保存。现在,当我尝试使用联接查询进行更新时,它在用户表中没有确切的unername的字段上不起作用, 注意表1中的名称bill gat,但用户字段中的名称是bill gates -她失踪了

SELECT * FROM table1;
+------+------+---------+
| id   | col1 | created_by|
+------+------+---------+
|    1 |    0 | steve jobs|
|    2 |    1 | bill gat  |
|    3 |    0 | Jones   |
+------+------+---------+
3 rows in set (0.00 sec)
我是这样解决的

    UPDATE table1 AS tr
JOIN users AS u ON u.name LIKE CONCAT('%', tr.created_by, '%')
SET tr.created_by=u.id
WHERE tr.created_by IS NOT NULL
    UPDATE table1 AS tr
JOIN users AS u ON u.name LIKE CONCAT('%', tr.created_by, '%')
SET tr.created_by=u.id
WHERE tr.created_by IS NOT NULL