【问题标题】:MySql update with join from another table [duplicate]MySql更新与另一个表的连接[重复]
【发布时间】:2017-05-23 05:10:45
【问题描述】:

我在 WorkSheetTransaction 表中添加了一个列,并希望使用根据 Department 表构建的名称来填充它。两个表都已经填充了连接字段 DepartmentId。

以下查询运行正常,但没有更新任何行。为什么不呢?

update WorkSheetTransactions
inner join Departments on WorkSheetTransactions.DepartmentId = Departments.DepartmentId 
set WorkSheetTransactions.DepartmentName = (Departments.GL_Account + '-' + Departments.DepartmentName)

我尝试了很多变体,但就是看不出哪里出错了。顺便说一句,连接字段在两个表中都是一个整数,所有其他 2 个字段都是 var_chars。

【问题讨论】:

  • 一般情况下,最好在应用级代码中处理数据显示的问题。让您的数据保持良好和规范化。

标签: mysql join field concat


【解决方案1】:

在mysql中,你应该使用concat来连接字符串:

UPDATE WorkSheetTransactions
INNER JOIN Departments
ON WorkSheetTransactions.DepartmentId = Departments.DepartmentId 
SET WorkSheetTransactions.DepartmentName = concat(Departments.GL_Account, '-', Departments.DepartmentName)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-07
    • 2014-05-13
    • 2014-06-02
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    相关资源
    最近更新 更多