Nitin Jain
Nitin Jain

Reputation: 93

Not function in Mysql

I am trying to use the NOT function to run Sql from table Projects .But it is giving Syntax error and the desired result is to get all data from table projects except where value in coloumn_2 is image

Coloumn_1 Coloumn_2 Coloumn_3  Type 
2335      Image     Value 1    Single
2335      Name 2    Value 2    NULL 
2346      Name 3    Value 3    MULTI
2234      Name 4    Value 4    Single
2235      Name 5    Value 5    Single

This is the current function using

public function get_all_projects()
{
    $params = array( ':affiliate_id' => $_SESSION['jigowatt']['user_id']);
    $sql = "SELECT * FROM Projects AND NOT (coloumn_2 = 'image')";
    $stmt = parent::query($sql, $params);
    while($row = $stmt->fetch(PDO::FETCH_ASSOC)) :
        $projects[] = $row;
    endwhile;
    return $projects;
}

Upvotes: 1

Views: 340

Answers (2)

Dylan Su
Dylan Su

Reputation: 6065

NOT should be put into WHERE clause.

If NULL in coloumn_2 is not desired, use:

SELECT * FROM Projects WHERE NOT (coloumn_2 = 'image')

If NULL in coloumn_2 is also desired, use:

SELECT * FROM Projects WHERE NOT (coloumn_2 <=> 'image')

Upvotes: 0

rmondesilva
rmondesilva

Reputation: 1792

desired result is to get all data from table projects except where value in coloumn_2 is image

This query may give you your desired result.

$sql = "SELECT * FROM Projects WHERE coloumn_2 != 'image'";

Upvotes: 1

Related Questions