开发者

MySQL Search For Names - Separated by Space

I have a MySQL database and a table which has a na开发者_如何学运维mes column as follows:

id  name           age
1   samuel lee     31
2   doug sokira    32
3   vernon smith   33

I would like to search through the database for names. I have this query so far:

SELECT * FROM table WHERE name LIKE 's%'

This would return me the first record with ID 1.

However, i would like the query to further do the search from the text separated by the space as well. So, to put in words the search should be done through both first name and last name.

I'm willing to stick to this database design where first name and last name are stored in one single column.

So, the above query should return rows 1, 2 and 3.

How should i formulate my query?


SELECT * From table WHERE name LIKE 's%' OR name LIKE '% s%'

Note that this cannot use any index and therefore will always be dog-slow. Please reconsider splitting the columns.


SELECT * FROM table WHERE name LIKE '%s%'

that would return row 1, 2, and 3 since they all contain an 's'.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜