SQL查询以搜索一个字符串 [英] SQL query to search in a concated string
本文介绍了SQL查询以搜索一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在数据库中,我有一个表prospect
,并有两列firstname
和lastname
.
In a database i have a table prospect
and has two columns firstname
and lastname
.
现在的问题是我想在这两列中进行搜索;最简单的解决方案是使用类似
Now the issue is that i want to search in both columns; the easy solution would be to use a query like
SELECT * FROM `prospect` WHERE lastname like '%piece of lastname%' or firstname like '%piece of firstname%'
但是,这需要具有两个搜索字段,名字和姓氏.我希望用户可以在一个字段中进行搜索.我想实现此查询时应该如何?
This however requires to have two search fields, firstname and lastname. I want that users can search in one field. How should a query look like when I want to achieve this?
推荐答案
您是要搜索两个字段的串联吗?然后,您可以使用类似的内容:
Do you mean you want to search the concatenation of two fields? Then you can use something like:
SELECT * FROM prospect
WHERE CONCAT(firstname,' ',lastname) LIKE '%ohn Smit%'
这篇关于SQL查询以搜索一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文