0

是否有可能必须更新一张表并从一个查询中插入另一张表?还是我在做梦?

我用查询

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

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

4

1 回答 1

0

不如看看

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

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

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

于 2013-06-19T04:02:22.543 回答