2016-09-19 47 views
9

出於某種原因,運行時,phpMyAdmin的返回90行:MySQL的更換影響0行,但在使用WHERE ... LIKE返回90

SELECT COUNT(*) 
FROM le_wp_posts 
WHERE post_content LIKE '%Â%' 

但以下更新3行只:

UPDATE le_wp_posts 
SET post_content = REPLACE(post_content, 'Â', '') 
WHERE post_content LIKE '%Â%' 

我也試過在UPDATE聲明中省略WHERE條款。是否有任何明顯的原因,我忽略這是造成這個問題?或者我可以採取哪些步驟來進一步調查原因?我的SQL不是最好的。

+0

你有沒有嘗試使用SELECT子句中的WHERE條件: 'WHERE POST_CONTENT IN(SELECT DISTINCT POST_CONTENT FROM le_wp_posts WHERE POST_CONTENT LIKE '%A%')' – Pat

+0

@pat'#1093 - 你無法指定目標表'le_wp_posts'在FROM子句中進行更新'是UPDATE的結果le_wp_posts SET post_content = REPLACE(post_content,'','')WHERE post_content IN(SELECT不同的post_content FROM le_wp_posts WHERE post_content LIKE' Â%')' – rickyduck

+0

http:// stackoverflow。com/questions/45494/mysql-error-1093 -cant-specified-target-table-for-update-in-from-clause - 檢查此事 – Pat

回答

0

能否請您嘗試使用如下JOIN:

UPDATE le_wp_posts l 
INNER JOIN (SELECT t.post_content 
      FROM le_wp_posts t 
      WHERE t.post_content LIKE '%Â%') t ON l.post_content = t.post_content 
SET l.post_content = REPLACE(l.post_content, 'Â', '') 
+0

確定在查詢中有一個錯誤,因爲'a'不是一個表,所以我把'a.post_content'改成'l.post_content'就像這樣:'UPDATE le_wp_posts l INNER JOIN(SELECT t.post_content FROM le_wp_posts t WHERE t.post_content LIKE'%Â'')t ON l.post_content = t.post_content SET l.post_content = REPLACE(t.post_content,'','')'然而0行再次生效。這是bizzare – rickyduck

+0

更新請再次檢查並感謝您的報告問題 – Susang

+0

錯誤:令牌不匹配現在:( – rickyduck

0

如果你有一個「身份證」,你可以試試這個方法:

UPDATE le_wp_posts 
SET post_content = REPLACE(post_content, 'Â', '') 
WHERE Id IN (SELECT * 
      FROM (
      SELECT Id 
      FROM le_wp_posts 
      WHERE post_content LIKE '%Â%' 
       ) as A 
      ) 
+1

您可能不需要使用'SELECT *'在'IN'子句中再次選擇子查詢 –

+0

@KamilG。你會得到沒有它的錯誤 – genespos

+1

[sql小提琴](http://sqlfiddle.com/#!9/5f5244/1)運行沒有錯誤。不同版本或什麼?我很好奇 –

3

我做了以下測試......

1)用一些數據創建表格:

create table test(col varchar(10)); 

insert into test values ('abc'), ('dbe'); 

2)使用相同的過濾器(但不同的字符)的行選擇數:

select count(*) 
from test 
where col like '%B%' -- note the uppercase 
; 

得到了以下結果:

 
+----------+                                         
| count(*) |                                         
+----------+                                         
|  2 |                                         
+----------+ 

1 row in set 

3)嘗試您的更新:

update test 
set col = replace(col, 'B', '') -- note the uppercase 
where col like '%B%' -- note the uppercase 
; 

並得到了這個結果:

 
Query OK, 0 rows affected (0.01 sec)                                   
Rows matched: 2 Changed: 0 Warnings: 0 

在我的情況下,創建表時使用的默認字符集和排序規則。默認字符集是'latin1'和整理'latin1_swedish_ci'。請注意排序規則末尾的ci ....它表示不區分大小寫。因此,LIKE篩選器進行了不區分大小寫的搜索,找到了2行,但REPLACE函數(可在文檔中看到)是區分大小寫的。可能與我的情況一樣,更新發現的行數與select中的行數相同,但由於REPLACE上的案例限制,數據更新較少。

如果這是你的問題,你不能只運行兩個更新,一個是大寫的情況下,一個是小寫的?我會嘗試開發一個更新的解決方案...

docs關於REPLACE(str, from_str, to_str)功能:

Returns the string str with all occurrences of the string from_str replaced by the string to_str. REPLACE() performs a case-sensitive match when searching for from_str.

docs的有關操作者LIKE

The following two statements illustrate that string comparisons are not case sensitive unless one of the operands is a case sensitive (uses a case-sensitive collation or is a binary string):

第一個例子:

 
mysql> SELECT 'abc' LIKE 'ABC'; 
     -> 1 

第二個例子:

 
mysql> SELECT 'abc' LIKE _latin1 'ABC' COLLATE latin1_general_cs; 
     -> 0 

注意排序結束時的cs。這意味着區分大小寫

+0

「如果這是你的問題,你不能只運行兩個更新,一個是大寫的情況下,一個是小寫的?」可能會爲一個角色工作,但長字符串標題案例呢? – Matt

3

如果你把一個UTF8編碼£C2A3,爲UTF8處理),並將其存儲到LATIN1列,當你讀回,你會得到£C2A3,因爲latin1的處理)。除去Â將可以處理大約32個字符,但對許多其他字符將失敗。 這將使表更難以修復!

讓我們看看你試圖存儲的一個例子,以及結束在表中的HEX。另外,讓我們看看SHOW CREATE TABLE來確認我的懷疑,它的目標是latin1

This討論了HEX調試技術。它討論了「最佳實踐」,其中包括在連接期間聲明你確實有utf8,而不是latin1。它談到「Mojibake」,舉例ñ變成ñ,使得REPLACE前景黯淡。

您的症狀與LIKE是符合字符集不匹配。

2

等不區分大小寫,但取而代之的是區分大小寫的,要使用下面的查詢旁路:

UPDATE le_wp_posts 
SET post_content = REPLACE(LOWER(post_content), LOWER('Â'), '') 
WHERE post_content LIKE '%Â%' 

或者如果你想最終的結果不會是小寫:

UPDATE le_wp_posts 
SET post_content = REPLACE(REPLACE(post_content, LOWER('Â'), ''), 'Â', '') 
WHERE post_content LIKE '%Â%' 
+0

大小寫不敏感取決於列的排序規則。 –

+0

@RickJames所以此查詢無效? –

+0

'LIKE'可以區分大小寫或不區分大小寫,具體取決於排序規則。 (我對你的第一句話不以爲然。)在我的回答中,我沒有看到「LIKE」與_real問題和solution_有任何關聯。 –

0

我想這個更新不是從PhpMyAdmin發生的,而是從客戶端發生的? 如果是這樣,它只是不同的區域設置。

0
--Query first selects original column as well as replacement string and then update original column 
Update Tbl1 
Set Tbl1.post_content=Tbl2.Replacement 
From le_wp_posts as Tbl1 
Inner Join 
(
    select post_content,REPLACE(post_content,'Â','') as Replacement 
    from le_wp_posts 
    where post_content like '%Â%' 
) as Tbl2 
On Tbl1.post_content=Tbl2.post_content 
1
--you just need to put N before string pattern too (if you want look for unicode char)*/ 
Update le_wp_posts 
Set post_content=REPLACE(post_content,N'Â','') 
where post_content like '%Â%' 
相關問題