mysql - 如果值是从另一个表中选择的,则更新表

标签 mysql

我有两个表:rec_new_license 和recruit_zips。

recruit_zips 包含“zip_code”和“office_name”,并包含邮政编码和办公室名称。

rec_new_license 包含字段“zip”和字段“recruit_office”。 如果“zip”中的值与recruit_zips 表中的“zip_code”匹配并且“office_name”与“Spring Hill”匹配,则需要更新此表的“recruit_office”字段。

查询均未执行任务或因错误而失败。我在这件事上做错了吗?

$sql = "UPDATE rec_new_license 
SET recruit_office = 'Spring Hill' 
WHERE zip IN 
( 
 SELECT zip_code FROM recruit_zips 
 WHERE office_name = 'Spring Hill'
)";

$results = $mysqli->query($sql);

还尝试过:

$sql = "UPDATE rec_new_license t1
  JOIN recruit_zips t2
    ON t1.zip = t2.zip_code
    WHERE t2.office_name = 'Spring Hill'
SET t1.recruit_office = 'Spring Hill'
";

最佳答案

你的语法错误。它必须是:

$sql = "UPDATE rec_new_license t1
  JOIN recruit_zips t2
    ON t1.zip = t2.zip_code
    SET t1.recruit_office = 'Spring Hill'
    WHERE t2.office_name = 'Spring Hill'
";

关于mysql - 如果值是从另一个表中选择的,则更新表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35752511/

相关文章:

PHP MySQL 编辑表单选择下拉列表预选

MYSQL使用JOIN从一张表中提取两列

Php过滤系统

php - 如何在 Laravel 中使用两个WhereIn

php - 将数据插入 Wordpress 数据库时出错。 SQLSTATE[HY093] : Invalid parameter number

android - android中通过url传递多个参数并在PHP中获取

php - 在 php 中生成带有换行符的 csv 时出现小故障

mysql - 简单的MySQL更新功能

mysql - 在 mysql 数据查询上获取不需要的输出 mysql

php - 在 LAMP 堆栈上显示表一与表二相比的唯一行