Mysql query with multiple conditions - mysql

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

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;

SQL-Print column name based on value and condition

I have been trying to print column names based on condition and value.
My problem is this
If two all the columns in the two rows like for data x and y have value yes the it should print those column names otherwise not
My code:
select 'A' from world.city where A = 'yes' AND data=y and data=x union all
select 'B' from world.city where B= 'yes' AND data=y and data=x union all
select 'C' from world.city where C= 'yes' AND data=y and data=x union all
select 'D' from world.city where D= 'yes' AND data=y and data=x union all
select 'E' from world.city where E= 'yes' AND data=y and data=x;
It is not giving perfect results.
Do a sum and union all then filter the rows with sum('yes') is equal to 2. See below.
select colname
from (
select 'A' as colname,sum(case when A='yes' then 1 else 0 end) col
from tbl where data in ('X','Y')
union all
select 'B',sum(case when B='yes' then 1 else 0 end)
from tbl where data in ('X','Y')
union all
select 'C',sum(case when C='yes' then 1 else 0 end)
from tbl where data in ('X','Y')
union all
select 'D',sum(case when D='yes' then 1 else 0 end)
from tbl where data in ('X','Y')
union all
select 'E',sum(case when E='yes' then 1 else 0 end)
from tbl where data in ('X','Y')) tab
where col = 2;
Result:
colname
B
D

How to Ignore Null Parameters in SQL Query

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)
....

SQL updating table

col1 | col2 | col3
-----------------------------
1 | 1 | somestring 12
1 | 2 | somestring sd
2 | 1 | somestring gvr
2 | 2 | somestring 46
3 | 1 | somestring sdf
3 | 2 | somestring 4
....
i want to UPDATE col3 WHERE col2 = 2
to be a CONCAT of col1 with col3
and REPLACE all ' ' with '-'.
Also if col1 < 10 add a leading '0' so '1' would be '01'
in total the value of col3 should be '01-somestring-sd'
can someone please help me with the statement?
Create data/insert data
CREATE TABLE Table1
(`col1` INT, `col2` INT, `col3` VARCHAR(255))
;
INSERT INTO Table1
(`col1`, `col2`, `col3`)
VALUES
(1, 1, 'somestring 12'),
(1, 2, 'somestring sd'),
(2, 1, 'somestring gvr'),
(2, 2, 'somestring 46'),
(3, 1, 'somestring sdf'),
(3, 2, 'somestring 4')
;
Query
UPDATE
Table1
SET
col3 =
CASE
WHEN col1 < 10
THEN REPLACE(CONCAT('0', col1, '-', col3), ' ', '-')
ELSE REPLACE(CONCAT(col1, '-', col3), ' ', '-')
END
WHERE
col2 = 2
Result
1 queries executed, 1 success, 0 errors, 0 warnings
Query: UPDATE Table1 SET col3 = CASE WHEN col1 < 10 THEN REPLACE(CONCAT('0', col1, '-', col3), ' ', '-') ELSE REPLACE(CONCAT(col1, '-',...
3 row(s) affected
Query
SELECT * FROM Table1
Result
col1 col2 col3
------ ------ ------------------
1 1 somestring 12
1 2 01-somestring-sd
2 1 somestring gvr
2 2 02-somestring-46
3 1 somestring sdf
3 2 03-somestring-4
UPDATE TABLE1 SET COL3=CONCAT(IF(COL1 < 10,0,''),COL1,REPLACE(COL3, ' ', '-' )) WHERE COL2=2
Try above code.
Hope this will helps.
Try this
Update table1
set
col3 = concat((case when col1>10 then '0'+col1 else col1 end),'-',REPLACE(COL3, ' ','-' ))
where col2=2
just use Concat and Replace
update table_name as t set t.col3 =
CONCAT(if(t.col1<10,0,''),t.col1,REPLACE(t.col‌​3,' ','-')) where
t.col2 =2;

I want to display one columns data sum on different columns based on where clause

Below is image which describe my requirements i have a column having different data i want its count on where condition where data=1 as col1 and where data = 2 as col2 and so on....
SELECT
COUNT(CASE data WHEN 1 THEN 1 ELSE NULL END) as Col1,
COUNT(CASE data WHEN 2 THEN 1 ELSE NULL END) as Col2,
COUNT(CASE data WHEN 3 THEN 1 ELSE NULL END) as Col3,
COUNT(CASE data WHEN 4 THEN 1 ELSE NULL END) as Col4
FROM yourTable
SELECT SUM(CASE WHEN data=1 THEN 1 ELSE 0 END) AS col1,
SUM(CASE WHEN data=2 THEN 1 ELSE 0 END) AS col2
FROM Table
Link to SQL Fiddle Example
SELECT
COUNT(CASE data WHEN 1 THEN 1 ELSE NULL END) as Col1,
COUNT(CASE data WHEN 2 THEN 1 ELSE NULL END) as Col2,
COUNT(CASE data WHEN 3 THEN 1 ELSE NULL END) as Col3,
COUNT(CASE data WHEN 4 THEN 1 ELSE NULL END) as Col4
FROM yourTable