MySQL使用REPLACE INTO与多个表

时间:2013-06-19 02:34:30

标签: mysql

是否有可能需要更新一个表而从一个查询中插入另一个表?或者我在做梦?

我用了查询

REPLACE INTO 
    ASSET_ASSOCIATION, 
    ASSET_HISTORY
JOIN 
     ASSET ON ASSET.ID = ASSET_ASSOCIATION.ASSET_ID
JOIN 
     HD_TICKET ON HD_TICKET.CUSTOM_FIELD_VALUE2 = ASSET.NAME
JOIN 
     HD_TICKET_CHANGE ON HD_TICKET_CHANGE.HD_TICKET_ID = HD_TICKET.ID
JOIN 
     USER U ON U.ID = HD_TICKET.SUBMITTER_ID
SET 
     ASSET_ASSOCIATION. ASSOCIATED_ASSET_ID = ( 
         SELECT 
             A.ID 
         FROM 
             ASSET A 
         WHERE 
             U.FULL_NAME LIKE BINARY CONCAT('%',A.NAME,'%') 
         AND 
             A.ASSET_TYPE_ID = 8 AND A.NAME <> ''),
     ASSET_HISTORY.TYPE_NAME = 'Computer', 
     ASSET_HISTORY.ASSET_ID = ASSET.ID, 
     ASSET_HISTORY.NAME = ASSET.NAME, 
     ASSET_HISTORY.TIME = NOW(), 
     ASSET_HISTORY.CHANGE_TYPE = 'Modification', 
     ASSET_HISTORY.FIELD_NAME = 'FIELD_46', 
     ASSET_HISTORY.VALUE1 = HD_TICKET_CHANGE.USER_ID, 
     ASSET_HISTORY.VALUE2 = HD_TICKET.SUBMITTER_ID, 
     ASSET_HISTORY.FRIENDLY_FIELD_NAME = 'User'
WHERE 
     HD_TICKET_CHANGE.DESCRIPTION LIKE '%Changed ticket Submitter%'
AND 
     ASSET_ASSOCIATION.ASSET_FIELD_ID = 46 
AND 
     ASSET.ASSET_TYPE_ID = 5
AND 
     HD_TICKET.ID = <TICKET_IDS>
ORDER BY 
     HD_TICKET_CHANGE.TIMESTAMP DESC
LIMIT 1

它充满了错误和非常错误,但只是为了给你一个想法..

1 个答案:

答案 0 :(得分:0)

相反看

INSERT INTO ... ON DUPLICATE KEY UPDATE ...

http://dev.mysql.com/doc/refman/5.6/en/insert-on-duplicate.html

不确定这是否允许实际操作两个表,但也许可以通过VIEW来实现。