user3589409
user3589409

Reputation:

Query with multiple WHERE's not working

My query selects all events from a certain zone (table = notifications) butT it makes sure that the events haven't been favorited by the user (table = favorites).

My query is not working and I'm not sure why (it currently still shows the events which the user favorited).

My code:

public function showCampusKT($uid)
            {
                $db = new db();
                $sql = "SELECT * FROM notifications 
                        WHERE 
                        n_beacon = 'Creativity Gym' 
                        OR
                        n_beacon = 'STIP'
                        OR
                        n_beacon = 'Cafetaria KruidTuin'
                        AND 
                        n_id !=
                        (                 
                            SELECT n_id
                            FROM favorites
                            WHERE u_id ='".$uid."'
                        )";
                $result = $db->conn->query($sql);
                return $result;
            }

Upvotes: 1

Views: 28

Answers (1)

potashin
potashin

Reputation: 44581

You should use in/not in:

$sql = "select * 
        from notifications 
        where n_beacon in ('Creativity Gym','STIP','Cafetaria KruidTuin') and 
              n_id not in ( select n_id
                            from favorites
                            where u_id = '".$uid."')";

Upvotes: 1

Related Questions