无法查询表中字段或列中的NULL或空值。

huangapple go评论63阅读模式
英文:

Cannot Query NULL or Empty Values in a Field or column in a table

问题

我有一个查询,它有99%的工作效果,但我无法查询NULL或空记录:

dbo.HUR_STAFF_DEMOGRAPHICS.DEMOGRAPHICS_POSITION = '' OR 
dbo.HUR_STAFF_DEMOGRAPHICS.DEMOGRAPHICS_POSITION IS NULL

我尝试了这两个条件,但是如果我添加了上面的这些条件,下面的SQL语句将不会返回任何行。然而,它确实会查询所有在数据库中输入了位置或职位信息的员工,但我的目标是查询所有在数据库中输入了位置或职位信息的员工,或者位置或职位信息为空或NULL的员工。

我单独查询了Demographics表,得到了总共1120条记录,我还计算了那些有空白或NULL(即没有职位头衔或位置)的列,共有316条记录。

果然,当我运行这个查询时,我得到了804条记录,这意味着查询没有查询到LocationPosition Title中的空列。

有人能看出为什么这个查询不会返回空列吗?

SELECT DISTINCT 
    dbo.HUR_STAFF_DEMOGRAPHICS.employee_no,
    first_name, last_name, PREFERRED_NAME,
    location,
    DEMOGRAPHICS_POSITION,
    EMPLOYEE_EMAIL,
    Location_name,
    Description
FROM
    dbo.HUR_STAFF_DEMOGRAPHICS,
    dbo.HUR_STAFF_INTERLINK,
    dbo.HUR_LOCATION,
    dbo.HUR_POSITION_CODE,
    dbo.HUR_EMAIL_ADDRESS
WHERE  
    dbo.HUR_STAFF_DEMOGRAPHICS.employee_no = dbo.HUR_STAFF_INTERLINK.employee_no
    AND dbo.HUR_STAFF_DEMOGRAPHICS.location = dbo.HUR_LOCATION.Location_no
    AND dbo.HUR_STAFF_DEMOGRAPHICS.DEMOGRAPHICS_POSITION = dbo.HUR_POSITION_CODE.POSITION_CODE
    AND dbo.HUR_STAFF_DEMOGRAPHICS.EMPLOYEE_NO = dbo.HUR_EMAIL_ADDRESS.EMPLOYEE_NO
    AND dbo.HUR_STAFF_DEMOGRAPHICS.employee_status = 'A'
    AND dbo.HUR_STAFF_DEMOGRAPHICS.DEMOGRAPHICS_POSITION IS NULL
ORDER BY 
    last_name 

提前感谢您的时间和专业知识。

英文:

I have a query that is 99% working, but I cannot query NULL or empty records for

dbo.HUR_STAFF_DEMOGRAPHICS.DEMOGRAPHICS_POSITION = '' OR 
dbo.HUR_STAFF_DEMOGRAPHICS.DEMOGRAPHICS_POSITION IS NULL

I tried both, but the SQL statement below returns no rows if I add the lines above. However it DOES query rows for ALL staff that have a location and or position entered into the database, but my goal is to query this information for all staff that have a location and or position in the database, or if have a location and or position is NULL or BLANK (empty).

I queried the Demographics table alone, and I get a total of 1120 records, I also calculated the columns that have BLANK or NULL (so no position title or Location) and that is 316 records.

Sure enough, when I run this query I get 804 records returned, which means the query is not querying Null or empty columns for Location or Position Title.

Can anyone see why the query will not return the null columns?

SELECT DISTINCT 
    dbo.HUR_STAFF_DEMOGRAPHICS.employee_no,
    first_name, last_name, PREFERRED_NAME,
    location,
    DEMOGRAPHICS_POSITION,
    EMPLOYEE_EMAIL,
    Location_name,
    Description
FROM
    dbo.HUR_STAFF_DEMOGRAPHICS,
    dbo.HUR_STAFF_INTERLINK,
    dbo.HUR_LOCATION,
    dbo.HUR_POSITION_CODE,
    dbo.HUR_EMAIL_ADDRESS
WHERE  
    dbo.HUR_STAFF_DEMOGRAPHICS.employee_no = dbo.HUR_STAFF_INTERLINK.employee_no
    AND dbo.HUR_STAFF_DEMOGRAPHICS.location = dbo.HUR_LOCATION.Location_no
    AND dbo.HUR_STAFF_DEMOGRAPHICS.DEMOGRAPHICS_POSITION = dbo.HUR_POSITION_CODE.POSITION_CODE
    AND dbo.HUR_STAFF_DEMOGRAPHICS.EMPLOYEE_NO = dbo.HUR_EMAIL_ADDRESS.EMPLOYEE_NO
    AND dbo.HUR_STAFF_DEMOGRAPHICS.employee_status = 'A'
    AND dbo.HUR_STAFF_DEMOGRAPHICS.DEMOGRAPHICS_POSITION IS NULL
ORDER BY 
    last_name 

Thanks in advance for your time and expertise.

答案1

得分: 1

我相信你的问题在于你正在使用DEMOGRAPHICS_POSITION = POSITION_CODE连接到DEMOGRAPHICS_POSITION表,但同时也在寻找DEMOGRAPHICS_POSITION = ''DEMOGRAPHICS_POSITION = NULL的值。假设DEMOGRAPHICS_POSITION表没有空的代码条目,对该表的连接将对这两个条件都失败,并且不会返回任何记录。

解决方案是将该连接更改为LEFT JOIN,但必须首先重写查询以使用ANSI连接语法。

使用表别名也是一种简化语句并提高可读性的良好实践。

尝试:

SELECT DISTINCT D.employee_no,
                first_name,
                last_name,
                PREFERRED_NAME,
                location,
                DEMOGRAPHICS_POSITION,
                EMPLOYEE_EMAIL,
                Location_name,
                Description
FROM HUR_STAFF_DEMOGRAPHICS D
JOIN HUR_STAFF_INTERLINK I
    ON D.employee_no = I.employee_no
JOIN HUR_LOCATION L
    ON D.location = L.Location_no
LEFT JOIN HUR_POSITION_CODE P
    ON D.DEMOGRAPHICS_POSITION = P.POSITION_CODE
JOIN HUR_EMAIL_ADDRESS E
    ON D.EMPLOYEE_NO = E.EMPLOYEE_NO
WHERE D.employee_status = 'A'
AND (D.DEMOGRAPHICS_POSITION = '' OR D.DEMOGRAPHICS_POSITION IS NULL)
ORDER  BY last_name

或者,对于这种特定的用法,你可以完全取消HUR_POSITION_CODE的连接,以及相应的选择列表引用(Description是否来自那里?)

英文:

I believe your issue is that you are joining to the DEMOGRAPHICS_POSITION table on DEMOGRAPHICS_POSITION = POSITION_CODE, but are also looking for DEMOGRAPHICS_POSITION = '' or DEMOGRAPHICS_POSITION = NULL values. Assuming that the DEMOGRAPHICS_POSITION table has no empty code entry, the join to that table will fail for both of these conditions and no record will be returned.

The solution is to change that join to a LEFT JOIN, but the query must be first rewritten to use ANSI join syntax.

Using table aliases is also a good practice to simplify the statement and improve readability.

Try:

SELECT DISTINCT D.employee_no,
                first_name,
                last_name,
                PREFERRED_NAME,
                location,
                DEMOGRAPHICS_POSITION,
                EMPLOYEE_EMAIL,
                Location_name,
                Description
FROM HUR_STAFF_DEMOGRAPHICS D
JOIN HUR_STAFF_INTERLINK I
    ON D.employee_no = I.employee_no
JOIN HUR_LOCATION L
    ON D.location = L.Location_no
LEFT JOIN HUR_POSITION_CODE P
    ON D.DEMOGRAPHICS_POSITION = P.POSITION_CODE
JOIN HUR_EMAIL_ADDRESS E
    ON D.EMPLOYEE_NO = E.EMPLOYEE_NO
WHERE D.employee_status = 'A'
AND (D.DEMOGRAPHICS_POSITION = '' OR D.DEMOGRAPHICS_POSITION IS NULL)
ORDER  BY last_name

Alternately, for this particular usage, you can drop the HUR_POSITION_CODE join entirely, along with the corresponding select list references (Is that where Description comes from?)

huangapple
  • 本文由 发表于 2023年2月16日 07:04:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466249.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定