如何在mysql查询的更新语句中加入多个表[重复]

Posted

技术标签:

【中文标题】如何在mysql查询的更新语句中加入多个表[重复]【英文标题】:how to join multiple tables in update statement of mysql query [duplicate] 【发布时间】:2015-02-01 01:29:03 【问题描述】:

我有表 my_employees,my_departments , my_emp_details , my_periods 下面的查询是在 oracle 的旧 join 语句中,我想将其转换为 mysql 语法,但我无法做到,请帮助

UPDATE my_employees a SET 
                               a.attribute1, a.attribute2, a.attribute3, a.attribute4,
                               a.attribute5, a.attribute6, a.attribute7, a.attribute8,
                               a.attribute9, a.attribute10, a.attribute11, a.attribute12,
                               a.attribute13, a.attribute14, a.attribute15, a.attribute16,
                               a.attribute17, a.attribute18, a.attribute19, a.attribute20 = 
                        (SELECT b.attribute1, b.attribute2, b.attribute3, b.attribute4,
                               b.attribute5, b.attribute6, b.attribute7, b.attribute8,
                               b.attribute9, b.attribute10, b.attribute11, b.attribute12,
                               b.attribute13, b.attribute14, b.attribute15, b.attribute16,
                               b.attribute17, b.attribute18, b.attribute19, b.attribute20
                          FROM my_departments b, my_emp_details c, my_periods p
                         WHERE b.dept_seq_id(+) = c.dept_seq_id
                           AND a.person_seq_id = c.person_seq_id
                           AND c.period_seq_id = p.period_seq_id
                           AND p.period_status = 'CURRENT')
                         WHERE a.person_seq_id = l_person_seq_id;

【问题讨论】:

【参考方案1】:

试试这个:

UPDATE my_employees a  
INNER JOIN my_departments b ON a.person_seq_id = b.person_seq_id 
INNER JOIN my_emp_details c ON b.dept_seq_id = c.dept_seq_id
INNER JOIN my_periods p ON c.period_seq_id = p.period_seq_id AND p.period_status = 'CURRENT' 
SET a.attribute1 = b.attribute1, a.attribute2 = b.attribute2, a.attribute3 = b.attribute3, 
     a.attribute4 = b.attribute4, a.attribute5 = b.attribute5, a.attribute6 = b.attribute6, 
     a.attribute7 = b.attribute7, a.attribute8 = b.attribute8, a.attribute9 = b.attribute9, 
     a.attribute10 = b.attribute10, a.attribute11 = b.attribute11, a.attribute12 = b.attribute12, 
     a.attribute13 = b.attribute13, a.attribute14 = b.attribute14, a.attribute15 = b.attribute15, 
     a.attribute16 = b.attribute16, a.attribute17 = b.attribute17, a.attribute18 = b.attribute18, 
     a.attribute19 = b.attribute19, a.attribute20 = b.attribute20;

【讨论】:

在更新声明中我认为不会有 from 关键字请检查一次

以上是关于如何在mysql查询的更新语句中加入多个表[重复]的主要内容,如果未能解决你的问题,请参考以下文章

MySQL查询日期在结果中加入汉字“年”“月”“日”

PYTHON 数据库mysql更新语句中加入变量?

在 like 语句中加入 SQL Server 表

如何在查询中加入 MS-SQL 和 MySQL 表?

Google App Script - 如何在 JDBC 查询中加入多个数据库

具有多个 AND 语句的 MySQL 查询 [重复]