Add ELSE in SELECT statement MySQL - 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);

Related

mysql test IF variable IS NULL (or empty)

we are selecting a row in mysql/mariadb and storing chosen columns in variables. After this, using IF we would like to test one of the variables and see if it has been set or is null, if it is then we assign it a value and continue on.
Using IS NULL does not seem to work on a non expression.
select id,history,active,jsonorder
INTO #id,#history,#active,#jsonorder
from myTable where uid = myUid
delimiter |
IF #jsonorder IS NULL THEN
#myNewVal="zzz";
ELSE
#myNewVal="yyy";
END IF|
insert into otherTable (colA) VALUES (#myNewVar);
What is the correct way to test if the select has provided a value into a variable such as #jsonorder?
We could use an expression in the SELECT list:
SELECT t.id
, t.history
, t.active
, IFNULL(t.order,1) AS `order`
INTO #id
, #history
, #active
, #order
FROM `myTable` t
WHERE t.uid = ...
Note that DELIMITER is not a SQL statement; it's command recognized by the mysql command line client (and some other clients).
documented here in MySQL Reference Manual: https://dev.mysql.com/doc/refman/8.0/en/mysql-commands.html
It's not clear in what context this script is being run; is this part of a MySQL stored program, or being called application code. What we are actually trying to achieve?
There are several other expressions we could use in the SELECT list, e.g.
CASE WHEN t.order IS NULL THEN 1 ELSE t.order END`
or
IF(t.order IS NULL,1,t.order)
et al.
FOLLOWUP
If we don't want to modify the original SQL statement; if we execute this:
SELECT t.id
, t.history
, t.active
, t.jsonorder
INTO #id
, #history
, #active
, #jsonorder
FROM `myTable` t
WHERE t.uid = ...
And then we want to perform an assignment to another user defined variable, based on a conditional test, we can achieve that in another SELECT or a SET statement.
For example:
SELECT #myNewVal := IF(#jsonorder IS NULL,'zzz','yyy') ;
ELSE 'yyy'
-or-
SELECT CASE
WHEN #jsonorder IS NULL
THEN 'zzz'
ELSE 'yyy'
END
INTO #myNewVal
-or-
SET #myNewVal := IF(#jsonorder IS NULL,'zzz','yyy')
The IF statement can only be used in Stored Routines. The IF function can be used virtually anywhere an expression can be put:
select id,history,active,jsonorder
INTO #id,#history,#active,#jsonorder
from myTable
where uid = myUid;
insert into otherTable (colA)
VALUES (IF(#jsonorder IS NULL, "zzz", "yyy"));
Where does myUid come from?
Is otherTable only one column wide? Or do all the other columns have defaults?
(I'm worried that you over-sanitized the question.)
This solve for cases where order is null, but not for when myUid doesnt exist in your table
SELECT id,history,active, COALESCE(order,1)
INTO #id,#history,#active,#order
FROM myTable
WHERE uid = myUid

How to change the column set in an update statement

I have a table that I need to update depending on whether a field is empty or not.
If it is empty, I need to set column b, else I need to set column c.
However, I can't seem to get it to work as the idea I had will result in a syntax error.
The following is the idea of the query that I have in mind:
UPDATE table_a SET
(IF a = '', 'b', 'c') = 'test';
I will also need to get a count of the rows updated, therefore I hope that I will be able to update the table by just using 1 update statement.
Thanks in advance
Use CASE statement like this:
UPDATE table_a
SET b =
CASE WHEN a = ''
THEN 'test'
ELSE b
END
, c =
CASE WHEN a <> ''
THEN 'test'
ELSE c
END
Working Demo: http://sqlfiddle.com/#!2/a823d/1
How it works:
Using CASE you check if a = '' then SET b = 'test otherwise SET b = b. Same is done with the column c.

SQL query with variable ,IF & select staement

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

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

MySQL Formating Strings or Returning Empty string on NULL

I am exporting a SELECT result to CSV via INTO OUTFILE. I have a series of simple SQL string functions I use to format the data. For instance: CONCAT('$',FORMAT(property.price,2)). I would like to otherwise return an empty string if the value is NULL, but I am unsure how to do both at the same time.
Also I am wondering the easiest way to take a TinyInt value of 0 or 1 and return "yes" or "no".
For tinyint you can use IF operator
select if(tinyint_value,'yes','no')
for first part you also can use if operator
select if(property.price is not null, CONCAT('$',FORMAT(property.price,2)),'')
To return yes, no or empty string depending on he value of the column you can use case as so:
select case column when 1 then 'Yes' else 'No' end as columalias ,
case when stringcolumn is NULL then '' else stringcolumn end as stringcolumn
from table
or
select case column when 1 then 'Yes' else 'No' end as columalias ,
IFNULL(stringcolumn,'') as stringcolumn
from table
You can use the coalesce() function. It returns the first of its arguments that's not NULL.
SELECT COALESCE(NULL, 1);
-> 1
SELECT COALESCE(2, 1);
-> 2