Get the App
SLTechnology News&Howtos  ›  Database  › 

Research on the problem of SQL query set returning to [null,null]

Shulou Source: shulou.com Published: 2022-06-01 05:29:17 09月21日 Update

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

Tags: Fields queries results just data database parts reasons solutions sometimes phenomena solutions projects Zhongtong problems research Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Xiaomi OPPO Reno macOS MariaDB