简体   繁体   中英

SQL SELECT where column CONTAINS substring

I have a database containing all usernames. Now, I have a page where I can search for an user. At the moment I use the following SQL:

SELECT uid, username, id, status 
FROM users 
WHERE `username` LIKE <search string>

Now, I have a record with the username Hattorius . But when I use that SQL syntax, and search for hatt . It doesn't give any results. How can I still make this work?

I searched some around, but nobody really had an answer to this.

Try to use LIKE :

SELECT uid, username, id, status 
FROM users 
WHERE `username` LIKE '%hatt%'

Remove the single quotes:

SELECT uid, username, id, status 
FROM users 
WHERE username LIKE '%hatt%'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM