检查用户是否存在 - MySQL

Checking if a user exists - MySQL

首先我知道这是重复的,但是我尝试了多个不同问题的答案并且 none 对我有用。这是我的代码:

IF EXISTS (SELECT * FROM user='username') select 1 else select 0

我的数据库设置为每个帐户都是其自己的列,因此有一个 table 填充了用户帐户行。我不知道这是否是专业的方法。所以建议会有所帮助和赞赏。

这是 phpMyAdmin 错误:(非常无用)

SQL query: Documentation

IF NOT EXISTS (

SELECT * 
FROM user =  '*'
)
SELECT 1 
ELSE SELECT 0

MySQL said: Documentation

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF not EXISTS (SELECT * FROM user='*') select 1 else select 0' at line 1 

尝试

SELECT IF( EXISTS(SELECT COUNT(*) FROM user WHERE username = 'yourname' ),1,0)

您的代码段

IF EXISTS (SELECT * FROM user='username') select 1 else select 0

无效 SQL。

假设您要统计 table 中名为 'user' 且用户名为 "johndoe" 的所有条目,并且用户名位于名为 'username' 的列中], 你会这样写:

SELECT COUNT(*) FROM USER WHERE USERNAME='johndoe';

进一步阅读: http://dev.mysql.com/doc/refman/5.6/en/select.html