简体   繁体   English

从表中选择*,其中column =值^ column2 =值

[英]select * from table where column = value ^ column2= value

I am creating a login system in PHP. 我正在用PHP创建登录系统。 I need a user to use either his or her username or email or phone number to login then the password. 我需要一个用户使用他或她的用户名或电子邮件或电话号码登录,然后再输入密码。 since I know in java we would do like email==user^ username == user does this apply in MySQL. 因为我在Java中知道,所以我们希望email==user^ username == user在MySQL中是否适用。 Could I do something like this in MySQL? 我可以在MySQL中做类似的事情吗?

Select * from user WHERE mail = 'user' ^ phoneNo= 'user' ^ username = 'user' and password = 'pass'

I have tried it and it failed. 我尝试过,但失败了。 Alternatively, I use multiple if s in PHP and check one at a time like this 另外,我在PHP中使用多个if ,并一次检查一次

if (mail == user){
}

The query would be - 查询将是-

Select * from user WHERE (mail = 'user' or phoneNo= 'user' or username = 'user') and password = 'pass'

Or 要么

Select * from user WHERE 'user' in (mail, phoneNo, username) and password = 'pass'

MySQL's xor does what ^ does in Java, but both are incorrect in this case. MySQL的xor完成^在Java中的功能,但是在这种情况下两者都不正确。 You want a user with either the username, phone number or email equal to a given string. 您想要一个用户名,电话号码或电子邮件等于给定字符串的用户。 xor means that only one of these can match , while, by the question's text, it seems as though you'd also want users who have, eg, both a username or a mail equal to 'user' . xor表示其中只有一个可以匹配 ,而根据问题的文本,似乎您也希望拥有(例如) usernamemail等于'user' For this usecase, a simple or operator should do the trick: 对于此用例,简单的or运算符应该可以解决问题:

SELECT * 
FROM   user 
WHERE  (mail = 'user' OR phoneNo = 'user' OR username = 'user') AND 
       password = 'pass'

Or, more elegantly, you could use the in operator as a shorthand for the series of or s: 或者,更优雅地讲,您可以将in运算符用作or s系列的简写:

SELECT * 
FROM   user 
WHERE  'user' IN (mail, phoneNo, username) AND 
       password = 'pass'

用这个

SELECT * FROM user WHERE (mail = 'user' OR phoneNo= 'user' OR username = 'user') AND password = 'pass';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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