Showing all column names where value is null - mysql

is there anyway i can show all column names where values are null?
For example i have table like this:
id
name
surname
1
Jack
NULL
2
NULL
Grain
3
NULL
NULL
And i want my result to look like that:
id
nullFields
1
name
2
surname
3
name, surname
Perfect solution would be some sql which takes all the columns and check them (if there wouldnt be need to manually input column name) but if there is no such possibility "normal solution" will do fine.

We can use the base string functions here:
SELECT id, CONCAT_WS(', ',
CASE WHEN name IS NULL THEN 'name' END,
CASE WHEN surname IS NULL THEN 'surname' END) AS nullFields
FROM yourTable
ORDER BY id;

Related

Define a custom ORDER BY order in mySQL using the LIKE word

How can I make a custom order where I sort the rows by NAME where the first rows' name has Bob in it followed by rows with name of Alex in it?
To explain what exactly I mean: I have made the following query to sort result if NAME = 'Bob' and if NAME = 'Alex':
SELECT * FROM table
ORDER BY CASE `NAME`
WHEN 'Bob' THEN 1
WHEN 'Alex' THEN 2
ELSE 3
END
But this only works when the NAME is exactly equal to Bob or Alex. I want to modify it to sort if the NAME has Bob or Alex in it, essentially if NAME LIKE '%Bob%' and NAME LIKE '%Alex%'. I tried something like the following but it does not work.
ORDER BY CASE `NAME`
WHEN LIKE '%Bob%' THEN 1
WHEN LIKE '%Alex%' THEN 2
ELSE 3
END
What is the correct syntax for this?
Use the other form of CASE where you specify a condition in WHEN rather than a value.
ORDER BY CASE
WHEN NAME LIKE '%Bob%' THEN 1
WHEN NAME LIKE '%Alex%' THEN 2
ELSE 3
END

Keep null values as '' after creating new table?

I have the following table:
cats
----------------------------------
name owner no_of_kittens
----------------------------------
bob hanna 1
tip frank
spark george 6
lucky rita
lady terry 3
----------------------------------
I want to create a new table the following way:
CREATE TABLE cats_with_kittens (
name VARCHAR(255) NOT NULL,
no_of_kittens VARCHAR(255)
);
INSERT INTO cats_with_kittens
SELECT name, count(no_of_kittens)
FROM cats
GROUP BY name;
The problem is that in the resulting table, the no_of_kittens values of tip and lucky is 1 instead of empty string as in the initial table.
Is there a way to create the table so that values with no no_of_kittens is empty string ''?
COUNT() will also count empty string, you can add case express to make it non-countable :
SELECT name, COUNT(CASE WHEN no_of_kittens <> '' THEN 1 ELSE 0 END) AS no_of_kittens
FROM cats
GROUP BY name;
INSERT INTO cats_with_kittens
SELECT name, case when count(no_of_kittens) = 0 then ''
else count(no_of_kittens)
end
FROM cats
GROUP BY name;
Here is a small demo

SQL: How to replace an entire field if it matches a string?

I am trying to replace an entire field from a database when it matches a certain string.
For example:
TABLE_FRUITS contains
ID NAME
---------------
1 APPLE
2 ORANGE
3 PASSIONFRUIT
4 BANANA
5 DRAGONFRUIT
6 KIWI
7 STRAWBERRY FRUIT
Now If I try to select column NAME, and if a field contains the string 'FRUIT', it should replace that whole field to another string, like 'SAMPLE'.
Expected result:
select NAME from TABLE_FRUITS;
would return:
APPLE
ORANGE
SAMPLE
BANANA
SAMPLE
KIWI
SAMPLE
I am not sure if I should use replace or substr/instr.
If you're using MySQL, you could use something like
SELECT IF(NAME LIKE '%FRUIT%', 'SAMPLE', NAME) AS NAME FROM TABLE_FRUIT
In other varieties of SQL you will probably need to use a CASE expression:
SELECT CASE WHEN NAME LIKE '%FRUIT%' THEN 'SAMPLE' ELSE NAME END AS NAME FROM TABLE_FRUIT
Use case when:
select case when name like '%fruit%' then 'Sample' else name end from tablename
You can use CASE
select case when name like '%FRUIT%' then 'sample' else name end
from table_fruit

Select columns that are not null in any record

I need to analyze a MySQL table and want to determine all columns that never contain NULL, 0 or an empty string in any record in that table.
I do not have a clue how to do that, since MySQL expects me to select the columns at the beginning of the statement. I thought I could maybe rotate the table by 90° and then do something like
SELECT column_header FROM rotated_table WHERE record_1 <> NULL AND record_2 <> NULL AND [...]
But this seems to be a lot of work.
Is there an easier way to get the information i require?
Update example:
Table1:
name street zip
MyName 1st Ave. 1000
OtherName 2nd Street NULL
My statement now should show something like:
name street
MyName 1st Ave.
OtherName 2nd Street
Because the column zip contains a NULL value.
If there was an additional row like
name street zip
MyName 1st Ave. 1000
OtherName 2nd Street NULL
NULL Foo blvd. 3453
It should return
street
1st Ave.
2nd Street
Foo blvd.
Because name and zip contain at least one NULL value.
You can use the behavior of COUNT ignoring NULL values to your advantage.
Subtract the count of the column you're examining from the count of the number of rows. Any column that returns a value of 0 does not contain a NULL value. You'll need to use a CASE statement to convert any values you consider to be "empty" into NULL.
This approach also eliminates the need copying the entire table in order to "rotate" it.
I whipped up an example here in SQLFiddle which should work for you.
Here's the content of my SQLFiddle example in case the link becomes unusable:
CREATE TABLE address
(
address int auto_increment primary key,
street1 varchar(20),
street2 varchar(20),
city varchar(20),
state varchar(20),
zip int,
comment varchar(20)
);
INSERT INTO address
(street1, street2, city, state, zip, comment)
VALUES
('123 Main St.', null, 'Cleveland', 'OH', 44123, ''),
('1313 Mockingbird Ln.', null, 'Cleveland', 'OH', 0, 'Unknown zip'),
('321 Main St.', 'Apt #1', 'Cleveland', 'OH', 44123, ''),
('321 Main St.', 'Apt #2', 'Cleveland', 'OH', 44123, '');
SELECT
COUNT(*) rows, -- not really needed, you can remove this
COUNT(*) - COUNT(CASE ad.street1 WHEN '' THEN NULL ELSE ad.street1 END) empty_street1,
COUNT(*) - COUNT(CASE ad.street2 WHEN '' THEN NULL ELSE ad.street2 END) empty_street2,
COUNT(*) - COUNT(CASE ad.city WHEN '' THEN NULL ELSE ad.city END) empty_city,
COUNT(*) - COUNT(CASE ad.state WHEN '' THEN NULL ELSE ad.state END) empty_state,
-- Change the value being compared based on the column type. Strings '', numbers 0, etc.
COUNT(*) - COUNT(CASE ad.zip WHEN 0 THEN NULL ELSE ad.zip END) empty_zip,
COUNT(*) - COUNT(CASE ad.comment WHEN '' THEN NULL ELSE ad.comment END) empty_comment
FROM address ad;
Which results in:
ROWS EMPTY_STREET1 EMPTY_STREET2 EMPTY_CITY EMPTY_STATE EMPTY_ZIP EMPTY_COMMENT
4 0 2 0 0 1 3
The correct comparison to NULL is IS NULL or IS NOT NULL. So:
SELECT column_header
FROM rotated_table
WHERE record_1 IS NOT NULL AND record_2 IS NOT NULL AND [...];
However "record" is an odd name for columns. You want to be sure the column names are in the WHERE clause.

Why won't if clause return nationality

In the following SQL select statement, I want nationality to print if name is null, and I have a SQL clause's if syntax working, but am not seeing nationality print out in the following:
table test2
id integer
case integer
nationality char(10)
name char(24)
1 1 france ""
2 2 england john
select t.id,
if(t.name is null, t.nationality, t.name)
as name_equivalent
from test2 t;
produces
id name_equivalent
1 ""
2 john
Why is that?
Thank You.
null is not equivalent to the empty string. The empty string '' is a valid form of data, null indicates there is no data present.
Try either
if (t.name is null or t.name='', t.nationality, t.name)
Or set the name in the first column to null
1 1 france null
You're checking if the name is null, but it was set to an empty string. To check for both, you need:
if(t.name is null or t.name='', t.nationality, t.name)
It looks like France's name field is actually an empty string, and not null. You need to also account for it being empty in your select statement. Such as t.name = ''