2010-10-15 22 views
0
UPDATE RIA.Contact SET title = REPLACE(title, 'Cheif', 'Chief') where title rlike 'Cheif'; 
UPDATE RIA.Contact SET title = REPLACE(title, 'Manger', 'Manager') where title rlike 'Manger'; 
UPDATE RIA.Contact SET title = REPLACE(title, 'Manging', 'Managing') where title rlike 'Manging'; 
UPDATE RIA.Contact SET title = REPLACE(title, 'Excutive', 'Executive') where title rlike 'Excutive'; 

我們可以合併上述4至是這樣的:UPDATE table SET A = IF(A > 0 AND A < 1, 1, IF(A > 1 AND A < 2, 2, A)) WHERE A IS NOT NULL;請幫合併4個的MySQL查詢到一個

我知道我們可以做到這一點使用的程序,但它可能只是使用查詢?

我想這一點 -

UPDATE RIA.Contact SET title = IF(title rlike 'Cheif', replace(title,'Cheif','Chief'), IF(title rlike 'Manger', replace(title,'Manger','Manager'))) WHERE title rlike 'Cheif' or title rlike 'Manger';

沒有去!

回答

2
UPDATE RIA.Contact 
SET title = REPLACE(
    REPLACE(
    REPLACE(
     REPLACE(title, 'Excutive', 'Executive'), 
     'Manging', 'Managing'), 
    'Manger', 'Manager'), 
    'Cheif', 'Chief') 
WHERE title rlike 'Cheif' 
    OR title rlike 'Manger' 
    OR title rlike 'Manging' 
    OR title rlike 'Excutive'; 
+0

這完全有效!我們可以使用If Then Else來使查詢更簡單嗎? – ThinkCode 2010-10-15 16:31:01

+0

我不認爲你可以在這種情況下使用If-Then-Else。 – 2010-10-15 16:39:53

+0

謝謝阿德里安!我只是希望MySQL有更多的功能,但我很滿意! – ThinkCode 2010-10-15 17:03:17