我对MySQL很新。考虑这个表:
ID INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
User_name VARCHAR(35) NOT NULL,
Full_name VARCHAR(55) NOT NULL,
User_vars VARCHAR(255) NOT NULL,
BirthDay DATE NOT NULL,
password CHAR(70) NOT NULL,
Security_hint VARCHAR(27) NOT NULL,
Email VARCHAR(225) NOT NULL,
userType ENUM ('a','b','c','d') NOT NULL DEFAULT 'a',
Signup_Date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
activation TINYINT(1) UNSIGNED NOT NULL,
Ip BINARY(16) NOT NULL,
PRIMARY KEY (ID),
UNIQUE KEY User_name (User_name,Email)
我正在使用以下查询来检查此表中是否存在电子邮件或用户名:
$query1 ="SELECT id FROM signup WHERE Email='$Email_Input' LIMIT 1";
$result1 = mysql_query($query1)
$query2 = "SELECT id FROM signup WHERE User_name='$User_name' LIMIT 1";
$result2 = mysql_query($query2);
if (mysql_num_rows($result2)>0){
echo '1';
}
else if (mysql_num_rows($result1)>0){
echo '1';
}
我的问题:是否有一种有效的方法只发出一个查询来检查两者的存在?
答案 0 :(得分:2)
是的,你可以这样做:
SELECT ID FROM signup WHERE Email ='$Email_Input' LIMIT 1
UNION ALL
SELECT id FROM signup WHERE User_name='$User_name' LIMIT 1
也就是说,如果这是两个不同的记录。如果它的记录相同,应该回答两个条件,只需:
SELECT ID FROM signup
WHERE Email ='$Email_Input'
AND User_name='$User_name'
LIMIT 1