Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL how to check if any value in a list of columns is NULL?

Tags:

sql

mysql

I would like to select the entries in a mysql table where there are NULL values, to later do an update or a delete on them:

select * from table where q is NULL;

But I have a rather long list of them, in this case all with a given prefix called pf_, and I want to select the entries where any of the columns is NULL. Can I do something like this?

select * from table where ANYISNULL(pf_q,pf_w,pf_e,pf_r,pf_t,pf_y,pf_u,pf_i,
                                    pf_o,pf_p,pf_a,pf_s,pf_d,pf_f,pf_g,pf_h,
                                    pf_j,pf_k,pf_l);

Any ideas how to do this other than having a very long query with lots of ORs?

like image 673
719016 Avatar asked Dec 15 '22 02:12

719016


1 Answers

You should do this:

select * from table where concat(pf_q,pf_w,pf_e,pf_r,pf_t,pf_y,pf_u,pf_i,
                                pf_o,pf_p,pf_a,pf_s,pf_d,pf_f,pf_g,pf_h,
                                pf_j,pf_k,pf_l) is null;

If any of the values is null, concat will return null.

like image 90
Michael Kruglos Avatar answered Dec 17 '22 14:12

Michael Kruglos