【问题标题】:How to check null values in multiple fields in where clause mysql如何在where子句mysql中检查多个字段中的空值
【发布时间】:2013-07-30 06:41:15
【问题描述】:

我正在尝试使用 where 子句匹配表中多个字段中的空值, 例如

 SELECT * FROM manage_users WHERE coalesce(surname, firstname, lastname, physical_address, postal_address, telephone_number, fax_number, cell_number, email,medical_aid_fund, medical_aid_number, allergies, emergency_contatct_person, emergency_contatct_number, id_number,gender, age_on_raceday, age_groups, shirt_size, type_of_club, roag_membership_number, csa_licence_number, provinical_running_licence_number, provinical_running_canoeing_number, tsa_licence_number, licence_number, number_of_comrade_marathon_completed, number_of_two_oceans_completed,    number_of_duzi_completed, number_of_nonstop_duzi_completd,number_of_amashova_completed, 
number_of_argus_completed, number_of_947_completed, number_of_midmar_mile_completed, make_of_running_shoes, make_of_road_bike, make_of_mtb, make_of_wetsuit) is NOT NULL and id='16'

我为此使用了合并,这是正确的方法。我没有得到预期的输出。谁能帮我得到结果。将不胜感激。

【问题讨论】:

  • 你期望什么输出?你会得到什么?
  • 预期结果是什么?尝试仅运行 SELECT * FROM manage_users where id = '16' 并查看您有哪些空值。 id字段是整数还是字符串类型?
  • MySQL coalesce() 函数返回列表的第一个非 NULL 值,如果没有非 NULL 值,则返回 NULL。
  • 我想从表中获取值,这些值应该包含提到的字段中的值(我的意思是提到的字段不应该为空)

标签: php mysql null where-clause


【解决方案1】:

COALESCE 返回参数列表中的第一个非空值。因此,如果任何字段不为空,您的查询将成功,它并不要求它们全部为非空。

您可以改为使用:

SELECT * FROM manage_users WHERE CONCAT(<list of columns>) is NOT NULL and id='16'

CONCAT() 与大多数普通函数一样,如果任何参数为 NULL,则返回 NULL。

要检查任何空值,请添加:

AND LEAST(<list of string columns>) != ''

这应该只是字符串列;如果你在LEAST中混合字符串和数字,它会将所有字符串转换为数字,任何不以数字开头的字符串都会转换为0,所以它不会做正确的测试。

【讨论】:

  • 我也想检查空值
【解决方案2】:

不接受 null 和空值的查询。它将选择提到的列在其中具有值的行。

Select * from manage_users where (surname and firstname and lastname and physical_address and postal_address) > ''

【讨论】:

    猜你喜欢
    • 2021-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-22
    • 1970-01-01
    • 2023-04-01
    相关资源
    最近更新 更多