dtgj8529 2016-04-23 11:28
浏览 73
已采纳

如何检查所有字段的记录不是NULL或空?

I have a record from a MySQL Database that needs to be validated to see if all the fields have a value or post some alert.

Is the best way to put all the field values into an array and then check the SUM where NOT NULL or Empty to see if it matches the qty of selected fields?

EXAMPLE: $result = | Value | NULL | NULL | Value | EMPTY |

In this example the Record has values from 5 Fields and 2 of those Fields are not Null or Empty

I need Validate to see when all 5 Fields have some value

  • 写回答

2条回答 默认 最新

  • duandu8707 2016-04-23 15:41
    关注

    You could use GREATEST (or LEAST):

    SELECT * 
    FROM   table1 
    WHERE  GREATEST(col1, col2, col3, col4) IS NOT NULL
    

    See SQL fiddle

    The idea is that GREATEST needs to check all of the arguments, and because expressions with NULL will return NULL (see here for exceptions to that rule), GREATEST will return NULL if any of its arguments is NULL.

    The above will filter the records that meat the condition, i.e. that do no have NULL values. With the same idea you could something that does not filter, but adds the information on whether it has a NULL or not:

    SELECT *, 
           CASE WHEN GREATEST(col1, col2, col3, col4) IS NULL 
               THEN 0 
               ELSE 1
           END AS is_OK
    FROM   table1 
    

    Now you will have an extra column in the result with either 0 or 1. It indicates whether your columns are all not NULL.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应