简体   繁体   English

Oracle:SQL根据同一表的另一行中的值更新同一表的行

[英]Oracle: Sql to update row of the same table based on value from another row of the same table

I need a sql to update the price column to 999 for the item records which meet the following condition: 我需要一个SQL将满足以下条件的商品记录的价格列更新为999:

  1. The location of the ITEM should not be null for both the records. 对于两个记录,ITEM的位置均不应为null。
  2. Update the price to 999 for which one of the 2 records has location as null. 将价格更新为999,其中2条记录之一的位置为空。
ID   LOCATION   ITEM   PRICE
1    NULL       101    0
2    123        101    0
3    NULL       102    0
4    NULL       102    0
5    124        103    0
6    NULL       103    0

OUTPUT TABLE SAMPLE 输出表样本

ID   LOCATION   ITEM   PRICE
1    NULL       101    999
2    123        101    0
3    NULL       102    0
4    NULL       102    0
5    124        103    0
6    NULL       103    999

Use IN (or EXISTS ) to check for the existence of a record with the item number where location is not null. 使用IN (或EXISTS )检查项目编号不为null的记录是否存在。

update items
set price = 999
where location is null
and item_id in (select item_id from items where location is not null);

I have created a table. 我已经创建了一张桌子。 Please find the query below. 请在下面找到查询。 Just execute and check. 只需执行并检查。 You should get the desired result as per your ask. 您应该根据自己的要求获得理想的结果。

CREATE TABLE TABLE1(ID INT, LOCATION INT, ITEM INT, PRICE INT)
INSERT INTO TABLE1 VALUES
(1, NULL, 101, 0), 
(2, 123 , 101, 0), 
(3, NULL, 102, 0), 
(4, NULL, 102, 0), 
(5, 124 , 103, 0), 
(6, NULL, 103, 0);

SELECT* FROM TABLE1;

WITH CTE AS
(
SELECT ITEM, COUNT(*) AS COUNTLOC FROM TABLE1 WHERE LOCATION IS NULL GROUP BY ITEM
)
UPDATE TABLE1
SET PRICE = 999 WHERE ITEM IN(SELECT ITEM FROM CTE WHERE COUNTLOC<2) AND LOCATION IS NULL;
update TABLE1 t1
INNER JOIN (select t12.item from TABLE1 t12 where t12.LOCATION is null
GROUP BY t12.ITEM 
HAVING count(t12.ITEM)=1)aaa on aaa.item = t1.ITEM
set t1.PRICE = 999 where t1.LOCATION is null 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用来自同一表中另一特定行的列值更新sql表中每一行的列 - How to update a column of every row in a sql table with the value of that column from another specific row in the same table 从同一张表的另一行更新一行? - Update a Row from another row of same table? oracle sql-根据另一个属性值更新同一表 - oracle sql - update same table based on another attribute value MSSQL:根据条件从同一表的另一行和另一列更新值 - MSSQL: Update value from another row and another column from the same table based on conditions 使用同一表中另一列的值更新每一行 - Update every row with a value from another column from same table SQL 如何根据相同的行值从另一个表插入? - SQL how to insert from another table based on same row values? 如何使用Apex Oracle SQL中另一个表的行值更新表行值? - how to update a table row value using a row value from another table in Apex Oracle SQL? 如何使用Apex Oracle SQL中另一个表的行值触发表行值的更新? - how to trigger an update on a table row value using a row value from another table in Apex Oracle SQL? 用Oracle中同一表中的其他行数据更新一行 - Update one row with other row data from same table in Oracle SQL查询以基于同一表中的另一行检索一行 - SQL query to retrieve a row based on another row in same table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM