我试图用另一个表中某个条件为真的值覆盖一个字段值。我在下面模拟了我的代码
伪代码:
upstream mydjangoback {
server 127.0.0.1:8000;
server 127.0.0.1:8001;
server 127.0.0.1:8002;
}
server {
listen 80;
location = /favicon.ico { access_log off; log_not_found off; }
root /home/raka/djwsgi;
server_name mydjango.com;
location /static/ {
root /home/raka/djwsgi;
}
location / {
proxy_pass http://mydjangoback;
}
}
我的代码:
Where an employee has the team Ops in old_data, get their new team from new_data and overwrite the team in old_data
但这会返回错误:
UPDATE old_data -- This has columns Employee, Latest_Team
SET
Latest_Team =
(select new_data.team
from new_data
left join old data
ON old_data.employee = new_data.employee
)
WHERE old_data.employee = 'Ops'
我不确定我到底要去哪里
答案 0 :(得分:0)
确保此查询不返回任何内容,并且您的代码可以正常工作
select new_data.team, count(*)
from new_data
left join old data WHERE old_data.employee = 'Ops'
group by new_data.team
having count(*) > 1
如果每个员工的Old_data.employee ='Ops'有多个团队,则sql不会知道为该员工选择哪个进行更新
答案 1 :(得分:0)
您的联接中有错字-“旧数据”应为“ old_data”。
也许这可以帮助您:update columns values with column of another table based on condition
在您的示例中为:
UPDATE old_data SET
Latest_Team = (SELECT new_data.team FROM new_data WHERE old_data.employee = new_data.employee LIMIT 1)
WHERE old_data.employee = 'Ops';
答案 2 :(得分:0)
如果您正在寻找 team “ ops”,那么您的查询有几个问题:
from
子句中有错字。所以,我想你想要
UPDATE old_data -- This has columns Employee, Latest_Team
SET Latest_Team = (select nd.team
from new_data nd
where old_data.employee = nd.employee
)
WHERE od.latest_team = 'Ops';
------^ I think this is the filter you describe