2014-06-24 110 views
1

我的表中有很多條目用戶。它包含多達500個電子郵件。在Mysql表中更新電子郵件

例子:

[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 

我想用 「1」 後綴更新所有電子郵件。 輸出應該是

[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 

這可能與更換的最後一次出現單查詢「」。 ???

+0

我不認爲這可以通過查詢來完成獨自一人。您可能需要使用PHP將'1'放在正確的位置 – asprin

+0

可以通過替換上次出現的「。」來完成。 – Manwal

+0

@Manwal不可能。如果這個ID是[email protected] – paradox

回答

-1

是可以這樣做的

select 
replace('[email protected]', 
     substring_index(substring_index('[email protected]','@',-1),'.',1), 
     concat(substring_index(substring_index('[email protected]','@',-1),'.',1),'1') 
) 

這裏有幾個測試

mysql> select 
    -> replace('[email protected]', 
    ->  substring_index(substring_index('[email protected]','@',-1),'.',1), 
    ->  concat(substring_index(substring_index('[email protected]','@',-1),'.',1),'1') 
    ->) as result ; 
+---------------------------------+ 
| result       | 
+---------------------------------+ 
| [email protected] | 
+---------------------------------+ 
1 row in set (0.00 sec) 


mysql> select 
    -> replace('[email protected]', 
    ->  substring_index(substring_index('[email protected]','@',-1),'.',1), 
    ->  concat(substring_index(substring_index('[email protected]','@',-1),'.',1),'1') 
    ->   
    ->) as result ; 
+------------------------------+ 
| result      | 
+------------------------------+ 
| [email protected] | 
+------------------------------+ 


mysql> select 
    -> replace('[email protected]', 
    ->  substring_index(substring_index('[email protected]','@',-1),'.',1), 
    ->  concat(substring_index(substring_index('[email protected]','@',-1),'.',1),'1') 
    ->   
    ->) as result ; 
+----------------------------------+ 
| result       | 
+----------------------------------+ 
| [email protected] | 
+----------------------------------+ 
1 row in set (0.00 sec) 

因此,這裏是更新命令

update your_table set email = 
replace(
email, 
substring_index(substring_index(email,'@',-1),'.',1), 
concat(substring_index(substring_index(email,'@',-1),'.',1),'1') 
) ; 
+0

在答案中增加了更新命令! –

+0

我已經測試了這個更新命令,它給出了「[email protected]」的「[email protected]」。小提琴 - http://www.sqlfiddle.com/#!2/6ed6bc/1。爲什麼這樣? – Manwal

+0

嗯,我知道,它試圖用test1替換test這個詞,所以一旦你有了[email protected],它們都會被替換,這對於一個真正的電子郵件ID來說不太可能發生,你可能永遠不會看到一個電子郵件ID爲'yahoo @ yahoo.com','gmail @ gmail.com' –

-1

最好的方法是使用腳本語言,可以很容易地做到這一點,分割字符串,添加'1',然後加入他們。我不知道如果它可能只與SQL。這是sql中的附加內容。 SQL query to prepend prefix to existing value in a field您也可以使用LIKE構造正則表達式來識別鏈接。還有Update a column of a table with append some values to the same column value in MySQL。我認爲你將不得不使用這些來創建一個變通辦法

+0

這不是我想要的。 :( – Manwal

0

它非常簡單使用CONCAT(),LENGTH()SUBSTRING_INDEX()。我在下面的查詢測試

UPDATE 
    your_table 
SET 
    email = CONCAT(
    SUBSTRING_INDEX(email, '.', 1), 
    '1.', 
    IF(
     (
     LENGTH(email) - LENGTH(REPLACE(email, '.', '')) 
    ) = 2, 
     SUBSTRING_INDEX(email, '.', - 2), 
     SUBSTRING_INDEX(email, '.', - 1) 
    ) 
) ; 

你可以參考here,上面MySQL的功能

這個解決方案甚至還可以很好的與

[email protected]com    [email protected] 
[email protected]    [email protected] 
[email protected]   [email protected] 
+0

這將失敗'選擇CONCAT(SUBSTRING_INDEX('[email protected]','。',1),'1',SUBSTRING_INDEX('[email protected]。 );作爲res;' –

+0

是的正確@AbhikChakraborty。它不適用於[email protected]。 – Manwal

+0

是嘗試匹配@,因爲電子郵件可以有一個@和然後做替換。 –

1

你可以這樣做:

UPDATE `USERS` SET `email` = CONCAT(SUBSTRING(`email`, 1, LENGTH(`email`) -1 -LENGTH(SUBSTRING_INDEX(`email`, '.', -1))), '1.', SUBSTRING_INDEX(`email`, '.', -1)); 

如果你必須考慮「co.uk」必須是一個牢不可破的單位,你可以使用它(並可能增加更多的這樣的子域的星座,如果你想):

UPDATE `USERS` SET `email` = IF(
SUBSTRING(`email`,-6)=".co.uk" 
, CONCAT(SUBSTRING(`email`,1,LENGTH(`email`)-6), "1.co.uk") 
, CONCAT(SUBSTRING(`email`,1,LENGTH(`email`)-1-LENGTH(SUBSTRING_INDEX(`email`, '.', -1))), '1.', SUBSTRING_INDEX(`email`, '.', -1)) 
); 
+0

試試這個'select CONCAT(SUBSTRING('[email protected]',1,LENGTH('[email protected]')-1 - LENGTH(SUBSTRING_INDEX('[email protected]','。',-1))),'1.',SUBSTRING_INDEX('[email protected]',' 。',-1))as res;' –

+0

@AhhikChakraborty這個人是如何工作的? – Manwal

+0

@Manwal如果你把'rskid.mo.re @ soundviewprep.co.uk'作爲你的電子郵件,上面會給你'rskid.mo.re @ soundviewprep.co1.uk',你無法控制它的電子郵件ID做任何事情。 –

相關問題