-2

我必须在我的数据库中获取一个联系人,但我的查询只返回我的联系人姓名中没有空格的地方,例如:“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";

它不返回,但两个联系人都在数据库中,有什么想法吗?

提前致谢。

已编辑我很笨,我的数据库崩溃了,我已经删除它并再次重新构建,现在选择正在工作!谢谢大家

4

1 回答 1

0

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

WHERE c.nome LIKE "%John%";

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

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

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

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

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

于 2012-07-15T23:03:07.690 回答