How can I run a MySQL query that selects everything that is not null? It would be something like
SELECT * FROM schedule WHERE ((all)) IS NOT NULL
I've just looked at your linked question and agree with Guffa's answer that you should normalise your database.
The above looks more like a spreadsheet then something that belongs in an RDBMS.
To answer your concerns about this being the way you want it displayed. You could write a pivot query and put it in a view to mimic your current table structure and use this for your display query.
This will avoid the need for some horrific 24 column WHERE clause whenever you want to search for data or find out if it is NULL as in this question.
Use XML so you don't have to iterate through all your columns:
--Your Source Table
CREATE TABLE [dbo].[schedule](
[id] [nchar](10) NULL,
[col1] [nchar](10) NULL,
[col2] [nchar](10) NULL,
[col3] [nchar](10) NULL
) ON [PRIMARY]
--I know my result should show items NOT NULL, like id:2,col1,val:'a'
INSERT INTO [schedule](id,col1,col2,col3)
values
(1,null,null,null),
(2,'a',null,null),
(3,null,'b',null),
(4,null,null,null),
(5,null,null,'c')
--temp placeholder table
CREATE TABLE #tempsch (id VARCHAR(max) not null, schColumns XML)
--Load temp table with XML version of your table
INSERT INTO #tempsch (id, schColumns)
SELECT s.id,
( SELECT *
FROM [schedule] AS x
WHERE x.id = s.id
FOR XML PATH('bar'), TYPE, ROOT('foo')
) AS xml_format_string
FROM [schedule] AS s
--This select should return all values Where all is NOT NULL
SELECT id,
sd.c.value('local-name(.)', 'VARCHAR(MAX)') AS elementName,
sd.c.value('.', 'VARCHAR(MAX)') AS elementValue
FROM #tempsch s
CROSS APPLY schColumns.nodes('/foo/bar/*') sd(c)
WHERE
sd.c.value('local-name(.)', 'VARCHAR(MAX)') <> 'id'
this works only with char and varchar type. If you are having other datatypes it throws error.
SELECT *
FROM SCHEDULE
WHERE coalesce(ID, FOO, BAR) IS NOT NULL;
inconsistent datatypes: expected CHAR
got DATE
Note: here FOO
is of date type.
It depend on what you mean exactly by "everything that is not null":
all columns must not be null
select * from schedule where col1 is not null AND col2 is not null AND ..
at least one column should be non-null
select * from schedule where col1 is not null OR col 2 is not null OR ..
At least this is how you would do it in "general sql". I don't know if MySql has special syntax for this.
You'll have to explicitly state the condition on each column, so e.g.
SELECT *
FROM schedule
WHERE id IS NOT NULL
AND foo IS NOT NULL
AND bar IS NOT NULL; -- and so on..
If you are using another programming language combined with sql, you could speed up this process by looping through a list of column names and using a parameter to do the if not null check rather than have to type them all in individually e.g
**code SQL**
Select *
FROM Schedule
WHERE @ColumnName IS NOT NULL
**code c#**
for(int i =0; i<parameterArray[].length; i++)
{
command.Parameters.Add(new SqlParameter("ColumnName", parameterArray[i]));
command.Execute();
}
I would suggest using transactions so you can do the above in one batch after the for loop has run through.