How to combine two update queries having different set and where clause?

jasim

I wish to optimize database access by combining this two queries, if possible.

How to combine this two statements into one?

update tbl_priority SET a=1 where j='j1' and priority='1';
update tbl_priority SET g=1 where j='j1';

Any help, suggestion is highly appreciated.

Tim Biegeleisen

I don't see a nice clean way of combining these two update statements, because the logic in the WHERE clause does not agree. However, we can retain the common condition j = 'j1' in the WHERE clause, and then use a CASE expression to handle the a assignement:

UPDATE tbl_priority
SET a = CASE WHEN priority = '1' THEN 1 ELSE a END,
    g = 1
WHERE j = 'j1'

Note that this is not ideal because we have a branch of the CASE expression which assigns a back to itself. This by itself should not cause a particular record to show up as having been modified though.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

combine two sql queries having each a with clause

How to combine two SQL queries with different where conditions?

how to combine two different queries?

TSQL combine two count queries with slightly different where clause into one output

Two SQL Server queries with different where clause

Combine WHERE and HAVING clause in a query

Combine two where queries

How to combine the results of two different queries with mongoose?

Two different queries on the same table with the same WHERE clause

two select queries with different where clause on same table

Combining two SELECT queries from same table but different WHERE clause

Merging two tables into one both the table having different where clause

How to combine two BigQuery queries where I've used different fields to group by?

Using HAVING in UPDATE with WHERE CLAUSE

combine two different queries into one

how to combine these two queries

How do I combine these two different LINQ queries into one?

How to combine the results of two different queries into a single query in SQL

how to combine results of two different queries from one table mysql

EF6: How to combine two different count() queries into one?

DART - how to combine two different Future based queries?

How to combine .where() queries in rails

Merge 2 queries with different WHERE clause

Pulling two separate queries based on a WHERE clause

How can i combine these queries into single query with where clause from another parent table?

How to combine two queries? MySQL

Postgresql - how to combine these two queries

Postgresql how to combine these two queries?

How to combine these two credentials Queries?