How to combine two update queries having different where condition - mysql

How can I combine this two update statements :
update Special_quota set Status=0,Additional_msg_quota=0 where User_id not in('1','2','3')
update Special_quota set Status=1,Additional_msg_quota=30 where User_id in('1','2','3')

You can use CASE expression like this:
UPDATE Special_quota
set Status = CASE WHEN User_ID IN('1','2','3') then 1 else 0 end,
additional_msg_quota = CASE WHEN User_ID IN('1','2','3') then 30 else 0 end

UPDATE Special_quota
set Status = CASE WHEN User_ID IN('1','2','3') then 1 else 0 end,
Additional_msg_quota = CASE WHEN User_ID IN('1','2','3') then 30 else 0 end

Related

SQL query to get value based on some values in fields

I have a MySQL database with a table named generations the structure of the table is as follows
I want to get value 10 as output when ten_generation have value 1 otherwise it will not return any value, 20 as output if twenty_generation have value 1 otherwise it will not return any value, 30 as output if thirty_generation have value 1 otherwise it will not return any value. If all the three fields has a value 1 output will be 10,20,30 also the task_id will provided as the input.
Its unclear what you intend the output to be when multiple generation columns are 1 but one solution is to use a CASE statement:
SELECT CASE
WHEN ten_generation = 1 THEN 10
WHEN twenty_generation = 1 THEN 20
WHEN thirty_generation = 1 THEN 30
ELSE NULL
END AS value
FROM generations
WHERE id = :your_id
If you want it as multiple columns then:
SELECT CASE
WHEN ten_generation = 1
THEN 10
ELSE NULL
END AS ten_value,
CASE
WHEN twenty_generation = 1
THEN 20
ELSE NULL
END AS twenty_value,
CASE
WHEN thirty_generation = 1
THEN 30
ELSE NULL
END AS thirty_value
FROM generations
WHERE id = :your_id
if only twenty_generation contain value 1 the output is 20 and if twenty_generation and ten_generation contain value 1 output is 10,20
Oracle Query:
SELECT TRIM(
LEADING ',' FROM
CASE WHEN ten_generation = 1 THEN '10' END
|| CASE WHEN twenty_generation = 1 THEN ',20' END
|| CASE WHEN thirty_generation = 1 THEN ',30' END
) AS value
FROM generations
WHERE id = :your_id
For MySQL you'd use CONCAT_WS:
select
concat_ws(',',
case when ten_generation = 1 then '10' end,
case when twenty_generation = 1 then '20' end,
case when thirty_generation = 1 then '30' end
) as result
from mytable
where task_id = 2;

Select * from table WHERE `column` = x if `column` != -1

I need to make a select like,
Select * from table WHERE column = x if column != -1
but i have no idea for now.
Anyone know or made in past something like that?
Thanks.
You should also write like this,
Select * from table
WHERE
1 = case when column != -1 then
case when column = x then 1 else 0 end
else 1 end
You can utilize case when in where clause.
Similarly you can add more conditional criteria like,
Select * from table
WHERE
1 = case when column != -1 then
case when column = x then 1 else 0 end
else 1 end
AND
1 = case when column1 [conditional operator] value then
case when column1 = xx then 1 else 0 end
else 1 end
This is just an example how you can integrate more conditional criteria together, even though you can add more case when in else part even.

Replace "0" for "1" and "1" for "0" in the same SQL query

I have a table named "emp" with the following data:
id name status
1 x 0
2 y 1
3 z 0
4 p 1
How to write a query to change status 0 into 1 and 1 into 0 in a single query?
With a CASE:
UPDATE emp
SET status = CASE status
WHEN 1 THEN 0
WHEN 0 THEN 1
END
Or, with a little math:
UPDATE emp
SET status = 1 - status
UPDATE
emp
SET
status = ABS(status - 1)
update your_table
set status = case when status = 1
then 0
else 1
end
update table set status = not status
true is an alias for 1 and false is an alias for 0 in MySQL
I am using this code. Its working same as you want.
if($_REQUEST['status']=="1")
{
$status="0";
}
else
{
$status="1";
}
$id=$_REQUEST['id'];
$q="UPDATE `emp` SET `status`='$status' WHERE `id` ='$id'";
$qr=mysql_query($q);
UPDATE `emp` SET `status` = (`status`^1)

Updating same flag twice using MYSQL

I have a table named chart with two columns, named UPC_REPORT_ID and UPC_FLAG
I am trying to prepare a query to update a UPC_FLAG=1 if UPC_REPORT_ID=1 and simultaneously I want to update UPC_FLAG=0 where UPC_REPORT_ID !=2
Please give me the suggestions
UPDATE chart SET UPC_FLAG = (CASE WHEN UPC_REPORT_ID=1 THEN 1 ELSE CASE WHEN UPC_REPORT_ID !=2 THEN 0 END END)
UPDATE chart
SET UPC_FLAG = CASE UPC_REPORT_ID
WHEN 1 THEN 1
WHEN 2 then UPC_FLAG
ELSE 0
END
Use WHEN
UPDATE ... set UPC_FLAG=CASE WHEN UPC_REPORT=1 THEN 1 WHEN UPC_REPORT_ID <> 2 THEN 0 END WHERE...
--dmg
The same thing with IF AS Barmar's answer
UPDATE chart
SET UPC_FLAG = IF(UPC_REPORT_ID = 1,1,IF(UPC_REPORT_ID <> 2,2,0))

How to do this in MySQL: if field value > 0 then minus one, else let it be

UPDATE tbl SET counts=counts-1 ...
If count is the only column you're updating (or, you don't have other criteria specified in your where clause), then you can just do that in the where clause
UPDATE [Table] SET counts = counts - 1 WHERE counts > 0;
However, if you're updating other columns in the same query, this won't work. But you have options
UPDATE [Table] SET counts = MAX(counts - 1, 0);
or
UPDATE [Table] SET counts = CASE WHEN counts > 0 THEN counts - 1 ELSE 0 END;
UPDATE tbl
SET counts=counts-1
WHERE counts > 0
thanks to #Peter Bailey
this is the example with the WHERE selector .
UPDATE [tbl_multimedia] SET [m_publish] = CASE WHEN [m_publish] = 0 THEN '1' ELSE '0' END WHERE id='1'
Good luck .