SQL query with variable ,IF & select staement - mysql

How can I create an SQL select query to return variable as true(1) if a column value exists in a table else false(0).
I want to use this variable in my scripts so that
if variable=1
execute A
ELSE
execute B

In Oracle you can use something like this
SELECT
CASE nvl(length(column1), 0)
WHEN 0 THEN 0
ELSE 1
END AS column1
FROM yourTableName;

You can try this...
IF ((SELECT COUNT(*) FROM TableName WHERE FieldName = 'Whatever') <> 0)
-- Execute something if column value exists
ELSE
-- Execute something if column value does not exist

SELECT CASE WHEN EXISTS(SELECT 1 FROM tableName WHERE fieldName='value') THEN 1;
ELSE 0;
END
INTO variable
--that is PostgreSQL

Related

How to show columns under certain condition in mySQL

I am (new to) working in MySQL but so far I was getting along OK. Now I'm stuck on the following:
I want to show a column in the query result but only if a certain condition is met. If the condition is not met then I would like the column not to show up in the query result (rather than show an empty column with only NULL values).
I tried to use a CASE WHEN statement but in that case it always shows the column in the query result, even if it is empty.
The problem is that I need to do this for 12 columns. The main reason is to avoid the query result to be cluttered with useless empty columns. The result of the query is to be processed by other people in excel.
Thanks in advance for your kind replies.
Although not ideal, you could use a prepared statement to only select columns where some rows are not null.
Suppose you have this table test where c is null for all the rows:
a
b
c
1
1
null
2
null
null
null
3
null
You can do this:
SET #query = 'SELECT ';
SET #query = CASE WHEN (SELECT COUNT(a) FROM test) > 0 then CONCAT(#query, 'a,') ELSE #query END;
SET #query = CASE WHEN (SELECT COUNT(b) FROM test) > 0 then CONCAT(#query, 'b,') ELSE #query END;
SET #query = CASE WHEN (SELECT COUNT(c) FROM test) > 0 then CONCAT(#query, 'c,') ELSE #query END;
SET #query = CONCAT(TRIM(TRAILING ',' FROM #query), ' FROM test');
Then:
PREPARE statement FROM #query;
EXECUTE statement;
Output:
a
b
1
1
2
null
null
3
Fiddle

mysql: Can I use an Case Expression to execute other statements?

SET #Count = '2';
SELECT CASE #Count
WHEN 1 THEN INSERT INTO customer (First_Name) VALUES (John)
WHEN 2 THEN INSERT INTO customer (First_Name) VALUES (Mary)
ELSE INSERT INTO customer (First_Name) VALUES (Ray)
END;
--this is a mysql case expression. Can I use it to execute other mysql statements like this? Actually want to execute a block of statements depending on the value of #count
You can move the logic within a single statement:
insert into customer(firstname)
select case #Count
when 1 then 'John'
when 2 then 'Mary'
else 'Ray'
end
Alternatively, you want a case statement rather than a case expression:
case #Count
when 1 then insert into customer(firstname) values('John');
when 2 then insert into customer(firstname) values('Mary');
else insert into customer(firstname) values('Ray');
end case;

Add ELSE in SELECT statement MySQL

So what Im trying is to add ELSE or the kind of condition that adds the exeception to the current rule. I have this before insert trigger right now:
SET new.perfId = (SELECT cust.webId FROM cust where cust.regTime=new.regTime);
I would like to add an ELSE to that statement, something like:
SET new.perfId = (SELECT cust.webId FROM cust where cust.regTime=new.regTime) ELSE blabla
How should this be set in order query is correct?
Thank you.
If you mean, that your condition is that the select doesn't return null, you can use coalesce().
SET new.perfId = COALESCE((SELECT cust.webId FROM cust where cust.regTime=new.regTime), 'blabla');
COALESCE() returns the first of its parameters which isn't NULL.
You could use COALESCE:
From the docs:
Returns the first non-NULL value in the list, or NULL if there are no
non-NULL values.
mysql> SELECT COALESCE(NULL,1);
-> 1
mysql> SELECT COALESCE(NULL,NULL,NULL);
-> NULL
You would end up with:
SET new.perfId = (SELECT COALESCE(cust.webId,"BLABLA")
FROM cust where cust.regTime=new.regTime);

MYSQL stored procedure, case

I've consulted this page: http://dev.mysql.com/doc/refman/5.1/en/case.html as well as this one, but can't get a simple procedure to work....
UPDATE:
To clearify what I want to do: I want to select all rows from a table where the field id is either 1, 0 or could be either of them. This is specified by an input parameter to the procedure, which takes values 0,1 or 2.
So if _id = 0 I want:
select * from TABLE where id = 0
If _id = 1 I want:
select * from TABLE where id = 1
And if _id = 2 I want:
select * from TABLE where id in (0,1)
I was hoping I could get the rest of it to work by myself if I only got the simple case-statement below to work...
What I want to do is something like:
begin
select * from TABLE where
case _id
when 0 then id=0
else id = 1
end as id
end
which gives error "You have an error in your SQL syntax".
I've also tried:
begin
select * from TABLE where
case _id
when 0 then id=0
else id=1
end case
end
which gives the same error. Obviously I've got the wrong syntax somewhere, but I can't figure out where... Can anyone help me out?
Thanks,
Niklas
Try this:
begin
select *,
case _id
when 0 then 0
else 1
end as id
from table
end
When used as part of a SELECT query, WHEN is not a statement, it's a control flow function.
You can also express this as:
begin
select *, _id != 0 as id
from table
end
build the query to look like this... mySQL case have a different syntax from C and Java...
set #input=2;
select * from foo
where
case when #input =2 then value in ( 1, 0) else
value = #input end;
live demo with sql fidle

simple flow control with mysql

I've simple example to describe my problem. Lets say I've table structured like this =>
create table A(
id INT(3) NOT NULL AUTO_INCREMENT PRIMARY_KEY,
act DATETIME,
act_reset INT(1) DEFAULT 0);
I want to write query with condition that if act_reset is true (so if not 0) then update column act DATETIME.
I've tried it like this=>
IF act_reset THEN UPDATE A SET act=now() WHERE id=1 END IF;
but this syntax is not valid, how I must write this condition with one query ? thanks
UPDATE A SET act=now() WHERE id=1 AND act_reset <> 0
Is this the query you are looking for?
Using If statement in MySQL :
IF act_reset <> 0 THEN
UPDATE A SET act=now() WHERE id=1
END IF;
You don't need to use the CASE statement or the IF statement in this case. Just move the condition If act_reset to the WHERE clause, like this:
UPDATE A
SET act = now()
WHERE id = 1
AND act_reset <> 0
Assuming act_reset is a variables name. You forgot semicolon, also you need to use DELIMITER
DELIMITER $$
IF act_reset <> 0
THEN
UPDATE A SET act=now()
WHERE id = 1 $$
END IF$$
DELIMITER ;
If act_reset is a column name then in that case it's not possible with IF clause instead try this:
UPDATE A
SET act = now()
WHERE id = 1 AND
act_reset <> 0;