Shivkesh Dwivedi
Shivkesh Dwivedi

Reputation: 17

Not getting result in MySQL when using !=, "AND", and "OR" within select query..!

I have a table, where I have fields like ID, username, address1, address2, zipcode, description etc. Well, I want to get all the rows as a result where 'ID' must not be equal to '$_SESSION['id']' and 'add1' must be equal to 'address1' or add2 must be equal to 'address2'.

My query is as follows :

$res = mysqli_query($con,"
SELECT * 
  FROM tabledata 
 WHERE ID !='$_SESSION['id']' 
   AND address1 = '$add1' 
    or address2 = '$add2' 
 ORDER 
    BY id DESC 
 LIMIT 5
");

But this query is giving me that row as well for which 'ID' value is equal to '$_SESSION['id']'.

Probably there would be any other way...but I could not figure it, any help or suggestion would be appreciated.

Upvotes: 1

Views: 58

Answers (1)

mscdeveloper
mscdeveloper

Reputation: 2889

Maybe Like This:

$res = mysqli_query($con,"
SELECT * 
  FROM tabledata 
 WHERE ID <> '$_SESSION['id']' 
   AND (
             address1 = '$add1'
          OR address2 = '$add2' 
   )  
 ORDER 
    BY id DESC 
 LIMIT 5
");

Upvotes: 1

Related Questions