How can I use if and else condition in mysql query. Here is my query.
SELECT CASE WHEN IDParent < 1 THEN 'no' ELSE 'yes' END AS ColumnName FROM tableName;
Try this;) Take a look of mysql-if-function.
SELECT If(IDParent < 1, 'no', 'yes') AS ColumnName FROM tableName;
Related
I have below insert into statement , for mysql. When i run select statement alone it brings results.
Create statement for table is below
**create table if not exists analyze_checks(dbname varchar(50),table_name varchar(50),frag_ratio decimal, days integer,needs_optimization char(3),needs_analyzing char(3)) ENGINE=INNODB**
**insert into analyze_checks(dbname,table_name,frag_ratio, days,needs_optimization,needs_analyzing )
(select
'test' as dbname,
table_name,
frag_ratio,
days,
needs_optimization,
needs_analyzing
from
(select
table_name,
cast(frag_ratio as decimal(5,2)) as frag_ratio,
days,
case when frag_ratio > 1 then 'Yes' else 'No' end as needs_optimization,
case when days > -1 then 'Yes' else 'No' end as needs_analyzing
from (
select
t.ENGINE,
concat(t.TABLE_SCHEMA, '.', t.TABLE_NAME) as table_name,
round(t.DATA_FREE/1024/1024, 2) as data_free,
(t.data_free/(t.index_length+t.data_length)) as frag_ratio,
datediff(now(), last_update) as days
FROM information_schema.tables t
left join mysql.innodb_table_stats s on t.table_name=s.table_name
WHERE DATA_FREE > 0 ORDER BY frag_ratio DESC )d ) d
where needs_optimization='Yes' or needs_analyzing='Yes');**
There is no need to wrap the SQL statement used for the INSERT in brackets. Heck, the whole query can be simplified like this:
INSERT INTO analyze_checks(dbname,table_name,frag_ratio, days,needs_optimization,needs_analyzing )
SELECT 'test' AS dbname,
tmp.table_name,
CAST(tmp.frag_ratio AS DECIMAL(5,2)) AS frag_ratio,
tmp.days,
CASE WHEN tmp.frag_ratio > 1 THEN 'Yes' ELSE 'No' END AS needs_optimization,
CASE WHEN tmp.days > -1 THEN 'Yes' ELSE 'No' END as needs_analyzing
FROM (SELECT CONCAT(t.TABLE_SCHEMA, '.', t.TABLE_NAME) AS table_name,
ROUND(t.DATA_FREE/1024/1024, 2) AS data_free,
(t.data_free / (t.index_length + t.data_length)) AS frag_ratio,
DATEDIFF(NOW(), last_update) AS days
FROM information_schema.tables t LEFT JOIN mysql.innodb_table_stats s ON t.table_name = s.table_name
WHERE DATA_FREE > 0) tmp
WHERE 'Yes' = CASE WHEN tmp.frag_ratio > 1 THEN 'Yes'
WHEN tmp.days > -1 THEN 'Yes'
ELSE 'No' END
ORDER BY frag_ratio DESC;
Brackets and derived tables are fine when used in moderation 🤐
I have the following code
set #string:='11';
select CASE #string
WHEN CAST(#a AS SIGNED)=11 THEN 'yes'
ELSE 'no'
END as filed;
I don't understand why it returns 'no'.
And also if I use WHEN #string='11' THEN 'yes' it also returns no.
Is this what you were aiming for?
set #string:='11';
select CASE WHEN CAST(#string AS SIGNED)='11' THEN 'yes' ELSE 'no' END as filed;
Can also be expressed as
set #string:='11';
select CASE CAST(#string AS SIGNED) when '11' THEN 'yes' ELSE 'no' END as filed;
set #string:='11';
select CASE WHEN CAST(#string AS SIGNED) = 11 THEN 'yes'
ELSE 'no'
END as filed;
Demo
Because you have not defined #a, so the value is NULL.
Hence, the ELSE clause is always going to be returned.
Then your second problem is that this does not work:
set #a = '11';
SELECT CASE #a
WHEN CAST(#a AS SIGNED) = 11 THEN 'yes'
ELSE 'no'
END as filed;
This is because CAST(#a AS SIGNED) = 11 is a boolean expression whose value is 0, 1 or NULL and that is never 11.
These do work:
SELECT CASE WHEN CAST(#a AS SIGNED) = 11 THEN 'yes'
ELSE 'no'
END as filed;
SELECT CASE #a WHEN 11 THEN 'yes'
ELSE 'no'
END as filed;
I have used the always true statement e.g. 1 = 1 in case statement of where clause in MYSQL with following syntax:
select * from tablename where
(case when tablefield is not null then
then tablefield = 'value'
else 1 = 1 end)
I want to know how can i use else 1 = 1 (always true statement) in sqlserver/tsql case statement in where clause.
you would not use case you would just write a multi conditional statement. In your case it would look like
Where (tablefield = 'value'
OR tablefield is null)
If you want to use the TSQL CASE function, you could do something like :
select * from tablename where 1 =
(case when tablefield is not null then
(case when tablefield = 'value' then 1 else 0 end)
else 1 end)
which could be simplified to :
select * from tablename where 1 =
(case when tablefield is null then 1 when tablefield = 'value' then 1
else 0 end)
You can leave out the "else 0" parts, as when no match is found, NULL is returned (which will not be equal to 1). i.e.:
select * from tablename where 1 =
(case when tablefield is not null then
(case when tablefield = 'value' then 1 end)
else 1 end)
select * from tablename where 1 =
(case when tablefield is null then 1 when tablefield = 'value' then 1 end)
Sorry I just mixed up the query,I was intended to ask for a condition when parameter variable is null instead of tablefield
In that case query may look like this :
select * from tablename where
(case when parameterfield_or_variable is not null then
then tablefield = 'value'
else 1 = 1 end)
or when using parameterfield for value
(case when parameterfield_or_variable is not null then
then tablefield = parameter_field_or_variable
else 1 = 1 end)
The answer as per asked question #KHeaney is right.
However the query in tsql/sqlserver as described in mysql, will be like this :
select * from tablename
where tablefield = #parameterfield
-- incase of comparing input parameter field with tablefield
or #parameter is null
so when #parameterfield will be null it will show all results otherwise it will restrict to only input value.
Thanks
You can try...
select * from tablename
where tablefield = isnull(#parameter, tablefield)
I know T-SQL and MySQL both provide COALESCE, which returns the first non-NULL value provided.
SELECT *
FROM tablename
WHERE (COALESCE(`tablefield`, 'value') = 'value')
is there a way of performing a count operation with SQL where the column equals X
and ideally a separate count for when the same column equals Y and again for Z?
SELECT yourcolumn, COUNT(*) AS cnt
FROM yourtable
WHERE yourcolumn IN ('X', 'Y', 'Z')
GROUP BY yourcolumn
Something like this ?
select
sum(case when val = 'x' then 1 else 0 end) as countX
,sum(case when val = 'y' then 1 else 0 end) as countY
,sum(case when val = 'z' then 1 else 0 end) as countZ
from table
I believe the following (shorter) statement should work for MySQL:
SELECT COUNT(val='X'), COUNT(val='Y'), COUNT(val='Z') FROM ...
I would suggest the following:
SELECT YourColumn, COUNT(<Any Column of your table>)
FROM YourTable
GROUP BY YourColumn
I am trying to get the count of females and males in the gender field of a table.
Is there a way to get the count of each in one query?
Something like:
select * from table count(where gender = 'm') as total_males, count(where gender = 'f') as total_females;
or will it require two queries?
select count(*) from table where gender = 'm';
select count(*) from table where gender = 'f';
This is basically a PIVOT. MySQL does not have a pivot so you can use an aggregate function with a CASE statement to perform this:
select
sum(case when gender = 'm' then 1 else 0 end) Total_Male,
sum(case when gender = 'f' then 1 else 0 end) Total_Female
from yourtable
See SQL Fiddle with Demo
Or using COUNT:
select
count(case when gender = 'm' then 1 else null end) Total_Male,
count(case when gender = 'f' then 1 else null end) Total_Female
from yourtable;
See SQL Fiddle with Demo
Something like this will work:
SELECT SUM(IF(t.gender='m',1,0)) AS total_males
, SUM(IF(t.gender='f',1,0)) AS total_females
FROM mytable t
The "trick" here is that we are using a conditional test to return either a 0 or a 1 for each row, and then adding up the 0's and 1's. To make this a little more clear, I am using the SUM aggregate function rather than COUNT, although COUNT could be used just as easily, though we'd need to return a NULL in place of the zero.
SELECT COUNT(IF(t.gender='m',1,NULL)) AS total_males
, COUNT(IF(t.gender='f',1,NULL)) AS total_females
FROM mytable t
Consider that the two expressions in the SELECT list of this query:
SELECT COUNT(1)
, SUM(1)
FROM mytable t
Will return the same value.
If you want to avoid the MySQL IF function, this can also be done using the ANSI SQL CASE expression:
SELECT SUM( CASE WHEN t.gender = 'm' THEN 1 ELSE 0 END )) AS total_males
, SUM( CASE WHEN t.gender = 'f' THEN 1 ELSE 0 END )) AS total_females
FROM mytable t
select sum(case when gender='m' then 1 else null end) as total_males, sum(case when gender='f' then 1 else null end) as total_females from ...
Should work just fine!
If your only issue is to avoid two queries, you can always write two queries as subselects of one query.
Select (select 1 from dual) as one, (select 2 from dual) as two from dual
This would work for your scenario, too.