Research on the problem of SQL query set returning to [null,null]
In the Java project, if you query some fields of a table in the database through Mybatis, the return value is list. Sometimes the [null, null] phenomenon occurs.
The specific reason is that the query SQL only fetches some fields, and these fields are null.
But for other fields of the database table, the query has a value, but it just so happens that the field you query is Null.
Execute: SELECT * FROM user2 WHERE name='rtr'
The result is:
A rtr 2019-02-19
B rtr 2019-02-19
Execute: SELECT department_id FROM user2 WHERE name='rtr'
The result is:
Null
Null
Execute: SELECT COUNT (*) FROM
(SELECT department_id FROM user2 WHERE name='rtr') AS t
The result is: 2.
Solution: several fields that need to be queried must be added with a judgment that is not empty.
SELECT department_id FROM user2 WHERE NAME = 'rtr' AND department_id IS NOT NULL
Results: null
SELECT
COUNT ()
FROM
(SELECT FROM user2 WHERE NAME = 'rtr' AND department_id IS NOT NULL) AS t
Result: 0