Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

sql - Checking if a string is found in one of multiple columns in 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...)

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%';

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%';

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

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

Any suggesstions please?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...