简体   繁体   中英

How to write conditional select query in mysql

I want to write a Login authentication query,

I want to check user Login and password if that is found in my database i fetch his details however if i cant find exact details i want to check if only the provided username exist in the table and get his name and profile pic.

i can do some thing like ..

SELCT * FROM `use_table` WHERE `username` = 'something' AND `password` = 'something'

if the above query returns empty rows then i fire another query

SELCT `name`, `profile_pic` FROM `use_table` WHERE `username` = 'something'

I can do this using multiple queries but the tricky part is i want to do this using a single query. ANY HELP ??

Use NOT IN :

SELECT `name`, `profile_pic` 
FROM `use_table` 
WHERE `username` = 'something' 
AND NOT IN (
    SELECT * FROM `use_table`
    WHERE `username` = 'something' AND `password` = 'something'
)

quit easy, you just use the second query, but with password return like this:

SELCT `name`, `profile_pic`,`password` FROM `use_table` WHERE `username` = 'something'

Then you do a compare with user input password and password that retured

if they matches, then login ok, else login failed! but you also get the user's information!

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