当两列的值在ORACLE中的同一个表中匹配时更新语句

时间:2014-10-20 07:06:02

标签: mysql sql oracle

如何编写查询以更新名称,城市和数量在Oracle中匹配的状态列。

ID NAME  CITY    QTY   STATUS
---------- ---------------------
1 ABC    CHN      50   0
2 XYZ    MUM      20   0
3 MNO    KOL      30   0
4 ABC    CHN      50   0
5 XYZ    MUM      20   0
6 MNO    KOL      40   0 
查询执行后的

结果

ID NAME  CITY    QTY   STATUS
---------- ---------------------
1 ABC    CHN      50   1
2 XYZ    MUM      20   1
3 MNO    KOL      30   0
4 ABC    CHN      50   1
5 XYZ    MUM      20   1
6 MNO    KOL      40   0

在给定的示例中,行1和4具有城市和名称相等的值。第2行和第5行也是相似的。

2 个答案:

答案 0 :(得分:2)

我猜你想要

UPDATE table_name a
   SET status = (SELECT count(*)
                   FROM table_name b
                  WHERE a.name = b.name
                    AND a.qty  = b.qty
                    AND a.id  != b.id);

答案 1 :(得分:1)

UPDATE TABLE_NAME as t_out
SET STATUS = '1'
WHERE EXISTS (SELECT 1 FROM TABLE_NAME as t_in
              WHERE t_out.NAME = t_in.NAME
                AND t_out.CITY = t_in.CITY
              GROUP BY NAME, CITY
              HAVING COUNT(*) > 1);