我想通过连接用户名中的4个字符和来自Employee表中的accountnumber的最后5位来填写用户表中的密码(另一个表)
我尝试了以下查询,但它返回空。
SET SQL_SAFE_UPDATES=0;
UPDATE user set password = (SELECT (CONCAT(SUBSTRING(user.username,1,4), SUBSTRING(employee.accountnumber,-5))) FROM employee WHERE user.employeename= employee.employeename);
SET SQL_SAFE_UPDATES=1;
当我在编辑器中单独尝试SELECT语句时,它会返回准确的值。
SELECT (CONCAT(SUBSTRING(user.username,1,4), SUBSTRING(employee.accountnumber,-5))) as pwd
FROM employee ,user WHERE user.employeename= employee.employeename;
xxxx03332
yyyy07674
但在UPDATE语句中它不起作用,因为我将密码列设置为'NOT NULL'选项。当我在编辑器中执行时,它显示错误为错误代码:1048。列'密码'不能为空 我试过下面的查询,但同样的错误
UPDATE user password , (SELECT (CONCAT(SUBSTRING(user.username,1,4), SUBSTRING(employee.accountnumber,-5))) FROM employee WHERE user.employeename= employee.employeename);
如果我在FROM子句中包含'user'(表名),则会将错误显示为错误代码:1093。您无法在FROM子句中指定目标表'user'进行更新 所以,我在UPDATE语句中删除了它。
答案 0 :(得分:1)
为什么这么复杂?这样做:
UPDATE user u
JOIN employee e ON u.employeename = e.employeename
SET u.password = Concat(Substring(u.username, 1, 4),
Substring(e.accountnumber, -5))
;