检查用户名是否存在

Whats the best way to check if a username exists in a MySQL table?

SELECT COUNT(*) as count FROM users WHERE username='whatever'

read out the first result row - if the 'count' column is > 0 then the username exists.

Alternatively, in php, you could use:

SELECT * FROM users WHERE username='whatever'

Then use mysql_num_rows (or the equivalent).

Create a stored procedure to do it. Its faster than running the SQL via your code. I cannot anything more than just comparing it with the saved data in your table. ;-)

SELECT 1 FROM `users` WHERE `username` = 'whatever' LIMIT 1

If you are trying to determine if a user exists in MySQL (i.e. a user name exists that can login to MySQL itself).

select user,host from mysql.user where user = 'username';

If you need to filter by host:

select user,host from mysql.user where user = 'username' and host = 'localhost';

These queries lets you see who has access to the MySQL database server and only is accessible if you are an administrator for a MySQL server.