检查是否在 mySQL 的多个列之一中找到字符串 [英] Checking if a string is found in one of multiple columns in mySQL

查看:38
本文介绍了检查是否在 mySQL 的多个列之一中找到字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查是否在一列或多列中找到一个字符串.

I need to check if a string is found in one or more columns.

基本上,我有一个程序可以让您检查多个字段(姓名、姓氏等...)

Basically, I have a program which lets you checks multiple fields (name, surname, etc...)

如果 name 和 surname 都被选中并且用户只输入名字,例如 chris,在 mySQL 中使用 LIKE 参数可以很容易地检查它,如下所示:

If both name and surname are checked and the user enters just the name, for example chris it would be easy to check it in mySQL with the LIKE parameter like this:

select * from tblClients WHERE name LIKE '%john%';

这显然有效.但是,我需要做的是,如果 name 和 surname 都被选中,它会做这样的事情:

This obviously works. However, what I need to do is that if both name and surname are checked it would do something like this:

select * from tblClients WHERE (name or surname) LIKE '%john%';

我希望如果在有一个名为 john doe 的客户端时执行此逻辑,第二个命令仍然会找到该客户端.

I want that if this logic is made when there is a client named john doe, the second command will still find that client.

我尝试了这个命令,没有发现语法错误,但是返回了 0 个结果.

I tried this command and no syntax errors were found, however, 0 results where returned.

有什么建议吗?

推荐答案

你不能只使用单独的 WHERE 子句,例如:

Can't you just use separate WHERE clauses, such as:

SELECT * FROM tblClients
WHERE (name LIKE '%john%')
OR (surname LIKE '%john%')

您必须根据用户选择的列来修改 SQL.

You're having to amend the SQL based on which columns the user selects anyway.

这篇关于检查是否在 mySQL 的多个列之一中找到字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆