Ignore parameter in WHERE clause if it is set to null - mysql

I have a table which looks like this:
id c1 c2 c3 c4
1 2 3 4 5
2 2 4 4 5
I have a query which selects an object from table based on values in columns:
SELECT * FROM table WHERE c1= '2' and c2 = '3' and c3 = '4' and c4 = '5'
How would I ignore a part of WHERE condition if it is set to null?
For example if I'd query like:
SELECT * FROM table WHERE c1='2' and c2= null and c3 = '4' and c4 = '5'
I'd get both objects from table. Thank you.

Since there are not any nulls in your columns you can use COALESCE() like this:
SELECT *
FROM tablename
WHERE c1 = COALESCE(#p1, c1)
AND c2 = COALESCE(#p2, c2)
AND c3 = COALESCE(#p3, c3)
AND c4 = COALESCE(#p4, c4)

if you want to completely ignore the null, you can check with in operator. you can use the COALESCE function.
SELECT * FROM table WHERE COALESCE(c1, '2') = '2' and COALESCE(c2,'3')='3' and COALESCE(c3, '4') = '4' and COALESCE(c4, '5') = '5'
This will match the c1 column for value 2 and nulls. in effect ignore any nulls.
However if you just want c2 to be ignored just use the coalesce on c2 alone

Related

CTE passing multiple subqueries

This is an extension to my previous post.
WITH cte1 AS (
SELECT * FROM Combination
WHERE Col1 = 'val' and city='karim'),
cte2 AS (
SELECT * FROM Combination
WHERE Col1 = 'val2' and city='karim')
SELECT CONCAT(cte1.Col2, cte2.Col2) AS Result
FROM cte1 CROSS JOIN cte2;
col1
col2
City
Val
145
Telang
val2
13
Telang
val2
25
Telang
val
146
karim
val2
124
karim
val2
56
karim
Output:
Result
14513
14525
146124
14656
There are multiple cities.I wanted to get combinations only for the values existing in the cities
Tried something like this, but does not work.
SELECT * FROM Combination
WHERE Column1 = 'value' and city IN(select city from Combinations);
Use an INNER self join of the table:
SELECT CONCAT(c1.Col2, c2.Col2) AS Result
FROM Combination c1 INNER JOIN Combination c2
ON c2.city = c1.city
WHERE c1.Col1 = 'val' AND c2.Col1 = 'val2';

Exclude rows if a column has different values for a combination of other columns

c1 | c2 | c3
----|-------|----
A | Z | false
A | Z | true
P | Y | false
Q | X | true
Output
---------------
P | Y | false
For the given table above, I'm trying to write a sql query that meets the below conditions :
If for a combination of c1 and c2, c3 has both false and true values - ignore those
rows.
Also ignore those rows whose c3 value is only true, for a combination of c1 and c2
Return those rows whose combination of c1 and c2 has the only value false in c3
What I tried :
To solve this problem, I tried looking at self-join and tried using intersect / except operators but that didn't help in any form.
You can do this with a combination of GROUP BY and CAST. First you can look for c1 and c2 combinations that occur only once, then you can filter for combinations that have a c3 of false.
SELECT c1, c2, MIN(CAST(c3 AS INT)) AS c3
FROM YourTable
GROUP BY c1, c2
HAVING COUNT(DISTINCT c3) = 1 AND MIN(CAST(c3 AS INT)) = 0
In MySQL, boolean is just tinyint(1) and true and false are just 1 and 0. You can group by c1, c2 and check that the sum of c3 is 0.
select c1, c2, sum(c3)
from test
group by c1, c2
having sum(c3) = 0
Demonstration

Sql - order by number of conditions satisfied

Suppose I have a query with multiple conditions that are in the OR relation like this:
SELECT * FROM TABLE WHERE (c1) OR (c2) OR (c3) OR ...
I do not want results to be ordered based on the number of condition they satisfied .this means records that satisfied c1 and c2 and c3 showed first(3 conditions),then the records that satisfy c1 and c2,or c1 and c3 ,or c2 and c3(2 conditions) and at the end records that satisfy c1 or c2 or c3
Make an expression that looks like this:
( CASE WHEN c1 THEN 1 ELSE 0 END
+ CASE WHEN c2 THEN 1 ELSE 0 END
+ CASE WHEN c3 THEN 1 ELSE 0 END)
It will provide you the number of conditions satisfied by the current record.

SQL select rows having count > certain number

I don't really know how to search for this, probably it's quite easy to do it, but I don't know how to do this.
I have a SQL table:
| c1 | c2 | c3 | c4 | c5 |
data data data data data
So I've 5 columns, and now I want to select the rows with only the following (c1, c2, c3) where that row appears more than 5 times in the table
Something like this:
Select c1, c2, c3
From table
having count(*) > 5 and (all in that count, all rows must have the same values on c1, c2, c3)
Can only do this with basical sql queries. Functions, declarations and etc are not allowed.
Don't really know if i'm explaining myself well.
Not absolutely sure I understand, but my guess would be
select c1, c2, c3
from <yourtable>
group by c1, c2, c3
having count(*) > 5
This query will return all records from your original table whose c1, c2, and c3 combined values appear in duplicate more than 5 times. I also included the actual count in the result set.
SELECT t1.c1, t1.c2, t1.c3, t1.c4, t1.c5, t2.cardinality
FROM yourTable t1
INNER JOIN
(
SELECT c1, c2, c3, COUNT(*) AS cardinality
FROM yourTable
GROUP BY c1, c2, c3
HAVING COUNT(*) > 5
) t2
ON t1.c1 = t2.c1 AND
t1.c2 = t2.c2 AND
t1.c3 = t2.c3
Just treat c1,c2,c3 as a single string c1+c2+c3:
SELECT c1, c2, c3 FROM table WHERE
c1 || c2 || c3 IN (
SELECT c1 || c2 || c3 FROM table
GROUP BY c1 || c2 || c3
HAVING COUNT(*) > 5);

Condition in Select statement….. T SQL

I have a table1 with columns as C1, C2, C3 and C4
All these columns stores bit value(true or false).
How to write a select query which uses the logical operations on these columns and gets me the final result?
Ex.:
Select ((C1 OR C2) AND (C3 OR C4)) AS FinalResult
from table1
Bitwise Operators are supported for bit columns:
Select ((C1 | C2) & (C3 | C4)) AS FinalResult
from table1
When both operands are bit, the result is going to be same as if logical operators were applied.
Just test to see if it is equal to 1 (true):
Select CASE WHEN (C1 = 1 OR C2 = 1) AND (C3 = 1 OR C4 = 1) THEN 1 ELSE 0 END AS FinalResult
from table1