OBTAIN NUMBER FIELDS MYSQL TABLE

2

Messrs. GOOD NIGHTS!

I want to get all the different null fields of a mysql table, example: select * from client where * client!="NULL"

Can you give me an orientation please?

    
asked by roca33scorpio 31.01.2018 в 01:44
source

2 answers

4

Short answer

The MySQL documentation indicates that to check for non-zero values IS NOT NULL should be used.

For example:

SELECT columna FROM tabla WHERE columna IS NOT NULL;

Long answer: the matter of nulls is not so obvious

When dealing with null values in MySQL, it is necessary to take into account what the documentation says in the section B.5.4.3 Problems with NULL Values :

The concept of the value NULL is a common source of confusion for newcomers to SQL, who often think that NULL is the same as an empty string '' . This is not the case. For example, the following statements are completely different:

INSERT INTO my_table (phone) VALUES (NULL);
INSERT INTO my_table (phone) VALUES ('');

Both statements insert a value in the telephone column, but the first inserts a NULL value and the second inserts an empty string. The meaning of the first can be considered as "the telephone number is not known" and the meaning of the second can be considered as "it is known that the person does not have a telephone and, therefore, does not have a telephone number".

To help with NULL handling, you can use the IS NULL or IS NOT NULL operators and the IFNULL () function.

In SQL, the NULL value is never true compared to any other value, including NULL. An expression that contains NULL always produces a NULL value unless otherwise stated in the documentation for the operators and functions involved in the expression. All the columns in the following example return NULL:

SELECT NULL, 1 + NULL, CONCAT ('Invisible', NULL);

To find column values that are NULL, you can not use an expr = NULL test. The following statement does not return rows, because expr = NULL is never true for any expression:

SELECT * FROM my_table WHERE phone = NULL;

To find NULL values, you must use the IS NULL test. The following statements show how to find the NULL phone number and the empty phone number:

SELECT * FROM my_table WHERE phone IS NULL;
SELECT * FROM my_table WHERE phone = '';

Conclusion

From the above, we can conclude that perhaps the verification:

SELECT columna FROM tabla WHERE columna IS NOT NULL;

is not enough ...

In many cases, more precision will be necessary and you may also have to check that the column is not blank , for example:

SELECT columna FROM tabla WHERE columna IS NOT NULL AND TRIM(columna)<>'';

In this way we evaluate that the column is not null and does not have an empty string.

    
answered by 31.01.2018 в 02:10
0

I give you an example with this table Persons :

We can see in people with id = 2 and id = 3 , the fields of address are empty, so its value is null .

If we make the following query:

SELECT * FROM Persons
WHERE Address IS NULL;

We obtain the data of people with ID = 2 and ID = 3 .

On the other hand, if we perform the query:

SELECT * FROM Persons
WHERE Address IS NOT NULL;

It shows us on the screen the information corresponding to people with ID = 1 and ID = 4 .

  

Here you have the corresponding documentation. Also this site has good examples and exercises that will help you practice.

Image obtained from: link

    
answered by 31.01.2018 в 02:08