【问题标题】:Update mysql table with select query from another database使用来自另一个数据库的选择查询更新 mysql 表
【发布时间】:2013-05-22 06:47:55
【问题描述】:

我有两个数据库,我想用另一个数据库表中的值更新一个表。 我正在使用以下查询,但它不起作用。

UPDATE database1.table1
SET field2 = database2.table1.field2
WHERE database1.table1.field1 = database2.table1.field1

我也尝试了以下查询,但它也不起作用:

UPDATE database1.table1
SET field2 = "SELECT field2 FROM database2.table1"
WHERE database1.table1.field1 = database2.table1.field1

【问题讨论】:

    标签: php mysql sql


    【解决方案1】:

    更新 1

    基于your commentmarkup 应该是连接的一部分。这是正确的:

    UPDATE oman.ProductMaster_T
        INNER JOIN main.ProductMaster_T 
            ON main.ProductMaster_T.ProductID = oman.ProductMaster_T.ProductID 
    SET oman.ProductMaster_T.Markup = main.ProductMaster_T.Markup
    

    您甚至可以添加ALIAS 来简化语句,

    UPDATE oman.ProductMaster_T o
        INNER JOIN main.ProductMaster_T m 
            ON m.ProductID = o.ProductID 
    SET o.Markup = m.Markup
    

    【讨论】:

    • "更新 oman.ProductMaster_T.Markup INNER JOIN main.ProductMaster_T ON main.ProductMaster_T.ProductID = oman.ProductMaster_T.ProductID SET oman.ProductMaster_T.Markup = main.ProductMaster_T.Markup" PL。告诉我哪里出错了
    • oman.ProductMaster_T.Markup -- 什么是Markup?表名还是列名?
    • oman 是数据库,ProductMaster_T 是表,Markup 是列名
    • 正是我喜欢 Stack 的原因...有一个粗俗的查询...即将提出一个问题,发现这个问题的表述比我能问的更好,答案也很好,谢谢@吴宇森!
    • 我收到错误代码:1111。此查询对组函数的使用无效。
    猜你喜欢
    • 1970-01-01
    • 2018-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多