never_had_a_name
never_had_a_name

Reputation: 93146

select rows with column that is not null?

by default I have one column in MySQL table to be NULL.

I want to select some rows but only if the field value in that column is not NULL.

what is the correct way of typing it?

$query = "SELECT *
        FROM names
        WHERE id = 1
        AND name != NULL";

Is this correct?

Upvotes: 3

Views: 15699

Answers (2)

Andy
Andy

Reputation: 17771

You should use:

$query = "SELECT *
        FROM names
        WHERE id = 1
        AND name IS NOT NULL";

You must use IS NULL or IS NOT NULL when working with NULL values

Upvotes: 8

Larry Lustig
Larry Lustig

Reputation: 50970

 AND name IS NOT NULL

(NULL comparisons require the special IS and IS NOT operator in SQL)

Upvotes: 8

Related Questions