更新一个表,再加一个表。
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 ...
附近使用正确的语法有什么想法吗?
感谢。
答案 0 :(得分:11)
UPDATE
语句中不应该有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 :(得分:0)
UPDATE table1 SET col1 = 1
from table1 t1
JOIN table2 t2 ON t1.ID = t2.ID
WHERE t1.Name = 'Test' AND t2.Age = 25;
答案 2 :(得分:0)
我在更新加入时遇到问题。在table1中我保存了用户名而不是userid。 对于用户名数据类型是varchar(10)。当名称超过10时,名称仅保存10个字符。现在,当我尝试使用连接查询进行更新时,它不适用于那些在users表中没有完全unername的字段, 请注意table1中的名称bill gat,但用户字段中的名称是bill gate - 缺少。
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
===我这样解决了
<!doctype html>
<html>
<body>
<div class="dropdown-notification text-center"> Welcome to All About History </div>
<nav>
<ul>
<li><a class="active" href="website.html">Home</a></li>
<li><a href="about.html">About Me</a></li>
<li><a href="people.html">People in History</a></li>
<li><a href="contact.html">Contact Me</a></li>
</ul>
</nav>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover:not(.active) {
background-color: #111;
}
.active {
background-color: #4CAF50;
}
.dropdown-notification {
height: 25vh;
position: absolute;
top: 0;
left: 0;
right: 0;
z-index: 999;
line-height: 40px;
background-color: #4CAF50;
animation: welcome-dropdown 2s ease, welcome-fadeout 2s 4s forwards;
text-align: center;
vertical-align: middle;
line-height: 25vh;
font-size: 70px;
}
@keyframes welcome-dropdown {
from {
transform: translateY(calc(-100vh));
}
to {
transform: translateY(0vh);
}
}
@keyframes welcome-fadeout {
from {
opacity: 1;
}
to {
opacity: 0;
height: 0vh;
}
}
</style>
</body>
</html>