Determine which part (s) of the WHERE clause

Let's say I have a SQL statement that checks for user input:

SELECT * FROM users 
WHERE username='test@example.com', password='abc123', expire_date>=NOW();

Is there a way in SQL to determine which WHERE conditions fail, without having to separately separate each condition in its own query and test it?

In this specific example, this will allow the developer to accurately indicate to users the reason for the failed login attempt.

For my purposes I use PHP / MySQL.

+3
source share
5 answers

Here is what I came up with:

SELECT
  IF(mem_username='test@example.com','true','Error: Bad Username') AS mem_username,
  IF(mem_password ='abc123','true','Error: Bad Password') AS mem_password'
FROM MEMBERS
WHERE mem_username='test@example.com' AND mem_password='abc123'

Thus, I can detect error messages in the code and then display them if necessary.

.. , , . , , . , , .

0

, , , , . , .

, , ; , // , -, ..

( , ), where. , , - :

SELECT *,
    CASE WHEN Password = 'asdf' THEN 1 ELSE 0 END AS IsPasswordMatch,
    CASE WHEN Expiration >= NOW() THEN 1 ELSE 0 END AS IsActiveAccount
FROM Users
WHERE Username = 'user'
+6

MySQL select-list. 1, true, 0, false.

SELECT password = 'abc123' AS is_authenticated,
       expire_date >= NOW() AS is_not_expired
FROM users
WHERE username='test@example.com';

note: , , , . CASE, .

PS: , . - . . ?

+4

, where , , . , , , ?

, , , , . , .

edit , :


  • : .

    :
    • = >
    • , 2.


  • : (, ) , , .

    :
    • = > /
    • Match = >

  • : ..
+1

, where ""

SELECT * FROM users 
WHERE username='test@example.com'
   And password='abc123'
   And expire_date>=NOW();
0
source

Source: https://habr.com/ru/post/1717461/


All Articles