I need to do an update query where I'm setting a value based on results from a count query. If the count returns no results, I want to set the status to 1, otherwise i want to set the status to 2.

Here's my attempt which doesn't work:

UPDATE TABLE_1 SET status = ((select count(1) from TABLE_2) > 0 ? 1 : 2)

Also, I'm doing this within a mybatis xml file, so if there is some magic I can do in mybatis that would work too.

有帮助吗?

解决方案

Hope DECODE Helps you! But all the rows in Status updated with same value.. You want it that way ?

UPDATE TABLE_1 SET status = (select DECODE(count(1),0,2,1) from TABLE_2);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top