简体   繁体   中英

mysql SELECT WHERE only specific columns have values

Good day

I have a table with a dozen columns and i'm looking for a query that will return rows that only have specific columns filled up. So it shouldn't return rows where 'illegal' columns are filled up.

Something like:

SELECT * FROM table WHERE col1 is NOT NULL 
AND all_other_cols is NULL;

^ i know it can be done by listing every other column and indicating "not NULL", but is there another way of doing this?

Hopefully this makes sense

UPDATE:

Based on the answers below I'll just use "DESCRIBE my_table;" then build a query that will filter all unwanted columns with 'is NULL'

There is no direct feature for that. But what you can do is this:

You concatenate all table columns to a single string and then test that string against the column that is not meant to be NULL . These two values can only be identical if all other columns are empty or hold NULL .

The answer to this question might help with how to even simplify that:

MySQL concatenating all columns

我认为您的方法是最简单的方法

您可以使用ISNULL条件: 在此处签出

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM