How to change the column set in an update statement - mysql

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.

Related

mysql: updating/adding to a column in the same table

I have a table
calls DATE(DATE) GENDER(VARCHAR(1)) NAME(VARCHAR(100))
I want to do this :
UPDATE calls WHERE GENDER="M" SET NAME = CONCAT("sir: ",NAME) ;
UPDATE calls WHERE GENDER="F" SET NAME = CONCAT("mrs: ",NAME) ;
but it is not working, How to do it ?
You can even do this in a single statement for both cases
update calls set name = case
when gender = 'f' then concat('mrs: ', name)
when gender = 'm' then concat('mr: ', name)
else name
end;
See this fiddle
https://www.db-fiddle.com/f/q2FtjGusyjdL2xPJuGtSJK/3
UPDATE calls SET NAME = CONCAT("sir: ",NAME) WHERE GENDER="M" ;
use set name before the where clause

SQL: Write a query that given a varchar, find if it exists in column A or column B

Suppose I have a table with two columns, A and B
CREATE table test(
A VARCHAR(20)
B VARCHAR(20)
);
INSERT into A
VALUES
('cat'), ('dog'),('cow'), ('horse')
INSERT into A
VALUES
('chicken'), ('steve'),('fish'), ('eel')
I want to write a query where given a varchar, it would return the column name where it exists (returns an empty varchar if neither). A and B does not have any identical varchars. I'm relatively new with mySql.
find('cat') returns 'A'
find('eel') returns 'B'
find('goat') returns ''
Use a WHERE to filter to the row containing your variable. Then a CASE statement for the logic.
SELECT
CASE
WHEN A = [your varchar] THEN 'A'
WHEN B = [your varchar] THEN 'B'
END
FROM test
WHERE A = [your varchar] OR B = [your varchar]
Note: this will return no rows if the variable is not present. And if the variable is present multiple times, it'll return one row for every occurrence. Finally, if it is found in both columns, this query will only return "A" since CASE statements are checked in order written.
Try this
SELECT
CASE WHEN A='input' THEN 'A'
WHEN B='input' THEN 'B'
ELSE '' END AS 'ColumnName'
FROM test WHERE A = 'yourString' OR B = 'yourString'
Simple case should do:
select
case when A='input' then 'A'
when B='input' then 'B'
else 'None' end as 'ColumnName'
from Table
I would do it like this
SET #c = 'Z';
SELECT
IfNull((SELECT 'A' FROM Test WHERE A = #c LIMIT 1), '')
+
IfNull((SELECT 'B' FROM Test WHERE B = #c LIMIT 1), '')
That way you'll only get 1 row, and it'll be 'A', 'B', 'AB' (if both), '' (if none)

Update multiple tables with single query, if a condition is met?

How do I update 2 tables in single UPDATE SET, under some condition ?
If the condition is not met, I'd only update one table..
I'd like to do something like this:
UPDATE tab1, tab2 SET
tab1.value2=7,
CASE tab1.value1 IS NOT NULL
WHEN true
THEN tab2.value1 = tab1.value1
END
WHERE tab1.id=1 AND tab2.id = tab1.tab2_fk_id
MySQL workbench complains: Syntax Error: Unexpected CASE_SYM
I think I should do this with a TRIGGER - function
Here how you can do it,
update tab1 ,tab2
join tab2 on tab2.id = tab1.tab2_fk_id
set
tab1.value2=7,
tab2.value1 = case when tab1.value1 IS NOT NULL then tab1.value1 else tab2.value1 end
where tab1.id=1
Many thanx to Abhik Chakraborty for a brillant solution. It will be surely useful in other context. I have though decided to create a trigger in static_conveyor (~ tab1 in the above example) to achieve equal behavior. I use real tables and column names here instead of generic.. :
USE `tca`;
DELIMITER $$
CREATE DEFINER=`root`#`localhost` TRIGGER `static_conveyor_AUPD` AFTER UPDATE ON `static_conveyor` FOR EACH ROW
BEGIN
IF (OLD.signal != NEW.signal)
THEN
IF ((NEW.pallet_id IS NOT NULL) AND (NEW.default_empty IS NOT NULL))
THEN
UPDATE pallet SET empty = NEW.default_empty WHERE pallet.id = NEW.pallet_id;
END IF;
END IF;
END
Works nice ..

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

How to make an Update subquery inside a CASE-WHEN logic in SQL SERVER?

I want to do an update on my table using the following CASE-WHEN logic:
UPDATE table1
SET Volume_eur =
CASE
WHEN TradeCode = 'A' THEN PaymentAmount_field1
WHEN TradeCode = 'B' THEN PaymentAmount_field2
WHEN TradeCode = 'C' THEN PaymentAmount_field3
END
* CurrencyRate/100
FROM table1
But I want to document which logic was used and stamp that on the Statusfield. So basically I want to do an update-statement inside my CASE-WHEN sentence like this: (marked with bold)
UPDATE table1
SET Volume_eur =
CASE
WHEN TradeCode = 'A' THEN PaymentAmount_field1
UPDATE table1 SET StatusField = 'Logic1'
WHEN TradeCode = 'B' THEN PaymentAmount_field2
UPDATE table1 SET StatusField = 'Logic2'
WHEN TradeCode = 'C' THEN PaymentAmount_field3
UPDATE table1 SET StatusField = 'Logic3'
END
* CurrencyRate/100
FROM table1
Is that possible, or how should I do it?
I would like to avoid copying the whole CASE-WHEN logic since it will become harder to maintain it.
If you don't want to use the case statement then make 3 seperate queries with different content like this:
UPDATE table1 set Volume_eur=PaymentAmount * CurrencyRate/100_field1
where TradeCode = 'A';
UPDATE table1 set Volume_eur=PaymentAmount * CurrencyRate/100_field2
where TradeCode = 'B';
UPDATE table1 set Volume_eur=PaymentAmount * CurrencyRate/100_field3
where TradeCode = 'C';
Hope this is helped you !!