在Mysql SELECT中不检索带空格的数据

时间:2012-07-15 22:21:42

标签: mysql select join

我必须在我的数据库中获得一个联系人,但我的查询只返回我的联系人姓名没有名称空格的地方,如:“John”,如果我有“John Locke”它不会返回,像这样的东西:

这是我的选择:

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 我是dumbass,我的数据库崩溃了,我已经删除它并重新建立,现在选择工作了! 谢谢大家

1 个答案:

答案 0 :(得分:0)

尝试使用LIKE匹配部分字段时,需要使用'%'通配符。在您的实例中: -

WHERE c.nome LIKE "%John%";

这将匹配John,John Lock等的记录。

我预计匹配'Bruce Waine'的失败将归结为数据中特定字段的开头或结尾处的其他字符,可能还有额外的空格,回车符或此类字符。尝试: -

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

请注意索引支持“LIKE”匹配程度不同。

如果这不能解决您的问题,那么查询可能并没有完全按照您的预期进行。