How to Ignore Null Parameters in SQL Query - mysql

Well I have a problem, and I don't even know that what I'm trying or thinking to do is possible or not.
I have a sql query:-
select *
from table
where (col1 >= '2016-07-05' and col2 <= '2016-07-07')
and col3 = ''
and col4 = ''
and col5 = ''
and col6 = '686486'
and col7 = ''
and col8 = '';
Ok I'm using this query to perform a search operation, i want it to be very specific that's why i'm thinking of doing it this way.
All these parameters can be null or can have any value, So all i want is to ignore those paramters whose value is null.
For Eg:- For above query the result should be same as
select *
from vehicle
where (col1 >= '2016-07-05' and col2 <= '2016-07-07')
and col6 = '686486';
Edit 1: First of all thanks for helping me out,I tried what was suggested
select *
from table
where (('val1' is null or col1 >= 'val1') and ('val2' is null or col2 <=
'val2'))
and ('val3' is null or col3 = 'val3')
and ('val4' is null or col4 = 'val4')
and ('val5' is null or col5 = 'val5')
and ('val6' is null or col6 = 'val6')
and ('val7' is null or col7 = 'val7')
and ('val8' is null or col8 = 'val8');
But I'm getting 0 rows as a result for this query, Am I doing something wrong.

Like this
...
and (#value3 is null or col3 = #value3)
and (#value4 is null or col4 = #value4)
....

Related

How to get count of columns that are having null values for a given row in sql?

I have a table that are having 115 columns.
Out of 7 columns I need to get the count of columns that are having not null values for a given row.
One method is to use case and +:
select t.*,
( (case when col1 is not null then 1 else 0 end) +
(case when col2 is not null then 1 else 0 end) +
(case when col3 is not null then 1 else 0 end) +
(case when col4 is not null then 1 else 0 end) +
(case when col5 is not null then 1 else 0 end) +
(case when col6 is not null then 1 else 0 end) +
(case when col7 is not null then 1 else 0 end)
) as cnt_not_nulls_in_row
from t;
In MySQL, this can be simplified to:
select t.*,
( (col1 is not null ) +
(col2 is not null ) +
(col3 is not null ) +
(col4 is not null ) +
(col5 is not null ) +
(col6 is not null ) +
(col7 is not null )
) as cnt_not_nulls_in_row
from t;
You may first query the given row from the table using the primary key and the use COUNT to count the number of columns from the queried row having null value, as follows:
WITH derived_row as
(SELECT col1, col2, col3, col4, col5, col6, col7 FROM table WHERE primary_key=key)
SELECT COUNT(CASE
WHEN col1 IS NULL THEN 1
WHEN col2 IS NULL THEN 1
WHEN col3 IS NULL THEN 1
WHEN col4 IS NULL THEN 1
WHEN col5 IS NULL THEN 1
WHEN col6 IS NULL THEN 1
WHEN col7 IS NULL THEN 1
END) AS null_column_count
FROM derived_row;

How to return a column value in case result is empty in SQL?

I am using a SQL query to fetch 3 column values. Sometimes I don't get records. What I need is a query that always returns field1, even if field2 and field3 are NULL.
I'm using field1 as input:
AND field1 IN('test1', 'test2', 'test3')
How can I always return these values?
Complete SQL query:
SELECT field1, field2, field3
FROM
(
SELECT
name1.field1,
name1.field2,
name3.field3,
name4.field4,
#pl2 := IF( #pl1 = name1.field1, #pl2, 1) as pl2,
#pl1 := name1.field1
FROM
(
SELECT
#pl1 := NULL,
#pl2 := 0
)
R,
db1.field5 AS name1,
db1.field6 AS name2,
db1.field7 AS name3,
db1.field8 AS name4
WHERE
name2.field9 = name1.field9
AND name3.field10 = name2.field10
AND name4.field11 = name3.field3
AND field1 IN('test1', 'test2', 'test3')
AND field12 LIKE 'helloworld'
AND name2.field13 LIKE 'helloworld'
GROUP BY name1.field1
ORDER BY name1.field14 desc
)
A
WHERE
pl2 = 1
Current output (wrong):
AND field1 IN('test1', 'test2', 'test3')
field1 | field2 | field3
test3 | hello | world
AND field1 IN('test1', 'test2')
field1 | field2 | field3
Needed output:
AND field1 IN('test1', 'test2', 'test3')
field1 | field2 | field3
test1 | [this part doesn't matter]
test2 | [this part doesn't matter]
test3 | hello | world
AND field1 IN('test1', 'test2')
field1 | field2 | field3
test1 | [this part doesn't matter]
test2 | [this part doesn't matter]
As a said in comment, I couldn't test as there are not (partial) script to CREATE TABLE and sample data insert.
I thought something like that: can you see if it works for you?
SELECT NAME1.FIELD1
, NAME1.FIELD2
, NAME3.FIELD3
, NAME4.FIELD4
, #pl2 := IF( #pl1 = NAME1.FIELD1, #pl2, 1) AS pl2
, #pl1 := NAME1.FIELD1
FROM FIELD5 AS NAME1
CROSS JOIN (SELECT #pl1 := NULL, #pl2 := 0) R
INNER JOIN DB1.FIELD6 AS NAME2 ON NAME1.FIELD9 = NAME2.FIELD9
LEFT JOIN DB1.FIELD7 AS NAME3 ON NAME3.FIELD10 = NAME2.FIELD10
LEFT JOIN DB1.FIELD8 AS NAME4 ON NAME4.FIELD11 = NAME3.FIELD3
WHERE FIELD1 IN ('test1', 'test2', 'test3')
AND name2.field13 LIKE 'helloworld'
GROUP BY NAME1.FIELD1
ORDER BY NAME1.FIELD14 DESC
;
What if you say SELECT COALESCE(field1, field2, field3) instead

How to update the first non null column of each row?

I have following solution
Select Col1 = Case when Col1 is null then 'update'
else Col1
,Col2 = Case when Col1 is null then Col2
when Col2 is null then 'update'
else Col2
,Col3 = Case when Col2 is null then Col3
when Col3 is null then 'update'
else Col3
.... and so on
Just wondering if anyone has better solution.
Your solution is not correct.
SQL does not works this way, you need to check all preceding columns.
(And you forgot ENDs for your CASEs)
DECLARE #T TABLE(
col1 sysname NULL
,col2 sysname NULL
,col3 sysname NULL
)
INSERT INTO #T
SELECT NULL, 'N', NULL
-- Incorrect
Select Col1 = Case when Col1 is null then 'update'
else Col1
END
,Col2 = Case when Col1 is null then Col2
when Col2 is null then 'update'
else Col2
END
,Col3 = Case when Col2 is null then Col3
when Col3 is null then 'update'
else Col3
END
FROM #T
-- Dull, but correct
SELECT
Col1 = ISNULL(Col1, 'update')
,col2 = CASE
WHEN Col1 IS NOT NULL
AND Col2 IS NULL
THEN 'update'
ELSE Col2
END
,col3 = CASE
WHEN Col1 IS NOT NULL
AND Col2 IS NOT NULL
AND Col3 IS NULL
THEN 'update'
ELSE Col3
END
FROM #T

Mysql query with multiple conditions

Simply put : I have a table with
columns : col1 col2 col3 col4 col5 col6 col7
datas : val1 2 toto t f f f
val1 2 toto t t f f
val1 2 toto f t f t
val2 3 tata t f f t
val5 4 tutu f t f f
i want a query to give me this : val1 2 toto t t f t (the condition is where col2 = 2) = > the first 3 lines in one line with t when t is found in any of the last 4 columns
Is there any way to achieve this in one query ?
True and false are internally 1 and 0. Therefore a simple
SELECT
col1, col2, col3, max(col4),
max(col5), max(col6), max(col7),
sum(col6 + col7) as one_of_them_is_greater_0_meaning_true
from your_table
group by
col1, col2, col3
will do.
EDIT: Since you're storing varchars (bad idea), replace each true/false column with
CASE WHEN col = 't' THEN 1 ELSE 0 END /*of course replacing column name appropriatelly*/
EDIT 2:
SELECT
col1, col2, col3, max(CASE WHEN col4 = 't' THEN 1 ELSE 0 END),
max(CASE WHEN col5 = 't' THEN 1 ELSE 0 END), max(CASE WHEN col6 = 't' THEN 1 ELSE 0 END), max(CASE WHEN col7 = 't' THEN 1 ELSE 0 END),
sum(CASE WHEN col6 = 't' THEN 1 ELSE 0 END + CASE WHEN col7 = 't' THEN 1 ELSE 0 END) as one_of_them_is_greater_0_meaning_true
from Table1
where col2 = 2
group by
col1, col2, col3
see it working live in an sqlfiddle
1 is true, 0 is false. You can replace that again if you want to. And use alias names for columns of course. It's just a proof of concept.
Ok then you can use
SELECT field1, field2, field3,
MAX(IF(field4 = 't',1,0)) as is_any_field4_true,
MAX(IF(field5 = 't',1,0)) as is_any_field5_true,
MAX(IF(field6 = 't',1,0)) as is_any_field6_true,
MAX(IF(field7 = 't',1,0)) as is_any_field7_true
GROUP BY field1, field2, field3

Proper way of conditionting with mysql to update different column with different conditions

column1 = value WHERE cond='1'
column2 = value WHERE cond='2'
In the example above.
Is this the right way to code it in mysql
UPDATE table SET (column1='value' WHERE cond='1') OR (column2='value' WHERE cond='2')
UPDATE
table
SET
column1 = (CASE cond WHEN '1' THEN 'value' ELSE column1 END)
, column2 = (CASE cond WHEN '2' THEN 'value' ELSE column2 END)
WHERE
cond IN ('1', '2')
;