2012-07-15 42 views
-2

我必須在我的數據庫中獲取聯繫人,但我的查詢只返回我的聯繫人名稱中沒有空格的名稱,例如:「John」,如果我有「洛克」它沒有回報,這樣的事情:在Mysql中沒有檢索到帶空格的數據SELECT

這是我的選擇:

SELECT c.nome AS nomeContato, c.cpf, c.email, c.grupo, c.informacoes_complementares, 
en.logradouro, en.bairro, en.numero, en.complemento, en.cep, en.municipio, en.uf, 
e.nome AS nomeEmpresa, e. cnpj, t.numero AS telefone 
FROM contato AS c 
INNER JOIN endereco AS en ON c.id_contato = en.id_contato 
INNER JOIN empresa AS e ON c.id_contato = e.id_contato 
INNER JOIN telefone AS t ON c.id_contato = t.id_contato 
WHERE c.nome LIKE "Homer"; 

在此選擇它返回的「荷馬史詩」的記錄,但如果我嘗試

SELECT c.nome AS nomeContato, c.cpf, c.email, c.grupo, c.informacoes_complementares, 
    en.logradouro, en.bairro, en.numero, en.complemento, en.cep, en.municipio, en.uf, 
    e.nome AS nomeEmpresa, e. cnpj, t.numero AS telefone 
    FROM contato AS c 
    INNER JOIN endereco AS en ON c.id_contato = en.id_contato 
    INNER JOIN empresa AS e ON c.id_contato = e.id_contato 
    INNER JOIN telefone AS t ON c.id_contato = t.id_contato 
    WHERE c.nome LIKE "Bruce Waine"; 

它不返回,但兩個聯繫人在數據庫中,有一些想法?

在此先感謝。

EDITED 我是笨拙的,我的數據庫崩潰了,我刪除了它並重新重新構建,現在選擇工作了! Thanks all

+0

你爲什麼用'LIKE'代替'='? – 2012-07-15 22:23:22

+0

看看你的標題!?! – markus 2012-07-15 22:25:41

+0

我試過使用「=」並且它不起作用:( – guisantogui 2012-07-15 22:26:36

回答

0

當試圖使用LIKE匹配部分字段時,您需要使用'%'通配符。在您的實例: -

WHERE c.nome LIKE "%John%"; 

這將匹配約翰,洛克等

我期望匹配失敗「布魯斯·韋恩」將下降到其它字符開頭或結尾記錄數據中的特定字段,可能還有額外的空格,回車符或此類字符。嘗試: -

WHERE c.nome LIKE "%Bruce Waine%"; 

請注意,索引支持不同程度的'LIKE'匹配。

如果這不能解決您的問題,那麼也許查詢不完全符合您期望的結果。