简体   繁体   中英

select * from table where column = value ^ column2= value

I am creating a login system in 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. Could I do something like this in 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

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. 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' . For this usecase, a simple or operator should do the trick:

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:

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';

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