更新查询问题

时间:2012-05-21 19:52:26

标签: mysql

这是我的更新查询。

    UPDATE sugarcrm.qb_salesorders_leads_c, sugarcrm.qb_salesorders
    SET sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsleads_ida = sugarcrm.qb_salesorders.memo, sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsqb_salesorders_idb = sugarcrm.qb_salesorders.id
    WHERE sugarcrm.qb_salesorders.id = sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsqb_salesorders_idb

当我运行它时,它会给我一个受影响的行0。

这是我的select语句,它使用WHERE语句中的相同信息。

    SELECT * from qb_salesorders_leads_c, sugarcrm.qb_salesorders 
    WHERE sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsqb_salesorders_idb = sugarcrm.qb_salesorders.id

这将返回354行,这是我期望在更新查询上更新的内容。我错过了什么请帮忙!

2 个答案:

答案 0 :(得分:1)

将隐式连接转换为显式连接:

UPDATE sugarcrm.qb_salesorders_leads_c leads 
    INNER JOIN sugarcrm.qb_salesorders orders
    ON orders.id = leads.qb_salesorders_leadsqb_salesorders_idb
SET leads.qb_salesorders_leadsleads_ida = orders.memo, 
    leads.qb_salesorders_leadsqb_salesorders_idb = orders.id

正如您所看到的,我还使用了别名来使SQL紧凑且易读。

答案 1 :(得分:0)

要使用连接进行更新,您必须使用显式连接语法:

UPDATE sugarcrm.qb_salesorders_leads_c
INNER JOIN sugarcrm.qb_salesorders
    ON qb_salesorders_leadsqb_salesorders_idb.id 
        = sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsqb_salesorders_idb
SET sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsleads_ida 
        = qb_salesorders_leadsqb_salesorders_idb.memo
    , sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsqb_salesorders_idb 
        = sugarcrm.qb_salesorders.id