Select rows with a column that is not null?

by default I have one column in the mysql table is NULL.

I want to select multiple rows, but only if the field value in this column is not NULL.

What is the correct way to enter it?

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

Is it correct?

+3
source share
2 answers

You should use (if $id- an integer):

   $query = "SELECT *
            FROM names
            WHERE id = '" . (int) $id ."'
            AND name IS NOT NULL";

You should use IS NULLor IS NOT NULLwhen working with null values

+7
source
 AND name IS NOT NULL

(for comparison, NULL requires a special IS and IS NOT-statement in SQL)

+8
source

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


All Articles