2015-08-16 126 views
0

OK,我有這樣的數據表(MyISAM數據):更新表連接的行

This is `table` 
    id | field1 | field2 | field3 | field4 | field5 
    1 | val1 | val2 | val3 | val4 | 1 
    2 | val1 | val2 | val3 | val4 | 1 
    3 | val2 | val1 | val4 | val3 | 1 
    4 | val2 | val1 | val4 | val3 | 1 

我需要更新這個表是這樣的(見我的WHERE條款):

CREATE TEMPORARY TABLE IF NOT EXISTS `table_temp` 
AS 
(
    SELECT 
     * 
    FROM 
     `table` 
    WHERE 
     `field7` IS NULL 
     AND 
     `field8` IS NULL 
     AND 
     `field9` IS NULL 
); 

UPDATE LOW_PRIORITY IGNORE `table` AS `t`, `table_temp` AS `t2` 
SET 
    `t`.`field7`=CURRENT_TIMESTAMP(), 
    `t`.`field9`=`t2`.`id` 
WHERE 
    `t`.`field1` = `t2`.`field2` 
    AND 
    `t`.`field2` = `t2`.`field1` 
    AND 
    `t`.`field3` = `t2`.`field4` 
    AND 
    `t`.`field4` = `t2`.`field3` 
    AND 
    `t`.`field5` = `t2`.`field5` 
    AND 
    `t`.`field7` IS NULL 
    AND 
    `t`.`field8` IS NULL 
    AND 
    `t`.`field9` IS NULL 
查詢完成後

,我有這樣的:

This is `table` after query 
     id | field1 | field2 | field3 | field4 | field5 | field9 
     1 | val1 | val2 | val3 | val4 | 1  | **3** 
     2 | val1 | val2 | val3 | val4 | 1  | 
     3 | val2 | val1 | val4 | val3 | 1  | **1** 
     4 | val2 | val1 | val4 | val3 | 1  | 

但我需要這個(需要更新所有的4行):

 id | field1 | field2 | field3 | field4 | field5 | field9 
     1 | val1 | val2 | val3 | val4 | 1  | 3 
     2 | val1 | val2 | val3 | val4 | 1  | **4** 
     3 | val2 | val1 | val4 | val3 | 1  | 1 
     4 | val2 | val1 | val4 | val3 | 1  | **2** 

實際上,所有的4行受到了影響,但使用IGNORE不只是一個簡單的關鍵字,field9UNIQUE指數。

至於我能理解,我的查詢每次從同一行開始在t2 - >所以我有IGNORE真的影響只有2行,而不是4.我的意思是,當系統即將更新第二排它從一開始看,第一個匹配的where子句是第3行 - >IGNORE,因爲當系統更新第1行時,第一個匹配行是id=3。我需要推動下一行的東西。

我嘗試設置另一個SET

... 
SET 
    `t`.`field7` = CURRENT_TIMESTAMP(), 
    `t`.`field9` = `t2`.`id`, 
    `t2`.`field7` = CURRENT_TIMESTAMP(), 
    `t2`.`field9` = `t`.`id` 
... 

但它並沒有幫助我。

有沒有辦法完成我所需要的?

+0

運行查詢並檢查它返回什麼 選擇 'field7' = CURRENT_TIMESTAMP(), 'field9' ='t2'.'id' FROM'table' as't','table_temp' as't2' WHERE 't'.'field1''''t2'.field2' AND 't'。 'field2' ='t2'.'field1' AND 't'.'field3' ='t2'.'field4' AND 't'.'field4' ='t2'.'field3' AND 't'.'field5' ='t2'.'field5 ' 和 't'.'field7' IS NULL 和 't'.'field8' IS NULL 和 't'.'field9' IS NULL –

+0

該查詢返回我8行用'NULL'每個領域。 – Vlad

+0

我想我應該使用'TRIGGER'或者'PROCEDURE'。誰能幫我這個? – Vlad

回答

0

您需要更新兩次 - 1升,然後 2.降

CREATE TABLE Table1 
    (
     `id` int, 
     `field1` varchar(4), 
     `field2` varchar(4), 
     `field3` varchar(4), 
     `field4` varchar(4), 
     `field5` int, 
     `field7` int, 
     `field8` int, 
     `field9` int UNIQUE 
    ) 
; 

INSERT INTO Table1 
    (`id`, `field1`, `field2`, `field3`, `field4`, `field5`) 
VALUES 
    (1, 'val1', 'val2', 'val3', 'val4', 1), 
    (2, 'val1', 'val2', 'val3', 'val4', 1), 
    (3, 'val2', 'val1', 'val4', 'val3', 1), 
    (4, 'val2', 'val1', 'val4', 'val3', 1) 
; 

CREATE TEMPORARY TABLE IF NOT EXISTS `table_temp` 
AS 
(
    SELECT 
     * 
    FROM 
     `table1` 
    WHERE 
     `field7` IS NULL 
     AND 
     `field8` IS NULL 
     AND 
     `field9` IS NULL 
    order by id 
); 

UPDATE LOW_PRIORITY IGNORE `table1` AS `t`, `table_temp` AS `t2` 
SET 
    `t`.`field7`=CURRENT_TIMESTAMP(), 
    `t`.`field9`=`t2`.`id` 
WHERE 
    `t`.`field1` = `t2`.`field2` 
    AND 
    `t`.`field2` = `t2`.`field1` 
    AND 
    `t`.`field3` = `t2`.`field4` 
    AND 
    `t`.`field4` = `t2`.`field3` 
    AND 
    `t`.`field5` = `t2`.`field5` 
    AND 
    `t`.`field7` IS NULL 
    AND 
    `t`.`field8` IS NULL 
    AND 
    `t`.`field9` IS NULL 
; 

CREATE TEMPORARY TABLE IF NOT EXISTS `table_temp2` 
AS 
(
    SELECT 
     * 
    FROM 
     `table1` 
    WHERE 
     `field7` IS NULL 
     AND 
     `field8` IS NULL 
     AND 
     `field9` IS NULL 
    order by id desc 
); 

UPDATE LOW_PRIORITY IGNORE `table1` AS `t`, `table_temp2` AS `t2` 
SET 
    `t`.`field7`=CURRENT_TIMESTAMP(), 
    `t`.`field9`=`t2`.`id` 
WHERE 
    `t`.`field1` = `t2`.`field2` 
    AND 
    `t`.`field2` = `t2`.`field1` 
    AND 
    `t`.`field3` = `t2`.`field4` 
    AND 
    `t`.`field4` = `t2`.`field3` 
    AND 
    `t`.`field5` = `t2`.`field5` 
    AND 
    `t`.`field7` IS NULL 
    AND 
    `t`.`field8` IS NULL 
    AND 
    `t`.`field9` IS NULL 
; 

select * from table1 
;