2015-09-25 91 views
1

我知道SQL,我能找到我通過替換WHERE條件

SELECT * from x6fnckq9h_posts 
where post_status = 'publish' and post_content like '%new.%' 

還需要信息,我知道我可以在現場與

update TABLE_NAME 
set FIELD_NAME = 
     replace(FIELD_NAME, 'find this string', 'replace found string with this string'); 

我需要的替換文本是:用''

所以更換每'%new.%',是不是送

update x6fnckq9h_posts 
    set post_content = replace(post_content, 'new.', '') 
WHERE post_status = 'publish' and post_content like '%new.%'; 
+1

需要'和POST_CONTENT LIKE「%新%」'部分 – JamieD77

+1

的LIKE部分是不是壞的,因爲它使交易規模下降。 (寫集等)保持它! – jarlh

+1

我不明白爲什麼它不起作用。 –

回答

1

如果你感到緊張,把更新成選擇和眼球輸出,看它是否看起來不錯:

select 
    post_content old_post_content, 
    replace(post_content, 'new.', '') new_post_content 
from x6fnckq9h_posts 
where post_status = 'publish' 
and post_content like '%new.%' 

如果你只要使用完全相同的喜歡提議的變更,以及where子句中,運行更新將會正常。

+0

對我來說很陌生。你能解釋一下爲什麼你打電話像[need_table] [strangename],替換...,[anotherstrangename]? 我只是不明白它%/ –

+0

@ЛысенкоАлексей語法是一個列*別名* - 它重命名返回的行集的列。我給他們起名,以便清楚地將兩列彼此區分開來。 – Bohemian

+0

明白了,謝謝;) –

0

而不是運行更新,只需做兩個字段的選擇。

SELECT post_content, replace(post_content, 'new.', '') 
FROM x6fnckq9h_posts 
WHERE post_status = 'publish' 
    AND post_content like '%new.%'; 
你不是真的