Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从select语句PL/SQL进行更新_Sql_Oracle_Sql Update - Fatal编程技术网

如何从select语句PL/SQL进行更新

如何从select语句PL/SQL进行更新,sql,oracle,sql-update,Sql,Oracle,Sql Update,我需要转换此选项: select * from (select * from game where rownum <= 4 order by rownum desc) where rownum = 1; 更新列x4和行4后,我想要什么表更改为0: x1 x2 x3 x4 x5 x6 1 2 1 6 2 2 1 2 2 2 3 3 2 3 3 5 5 3 2 3 2 4 5 4

我需要转换此选项:

select *
    from (select * from game where rownum <= 4 order by rownum desc)
  where rownum = 1;
更新列x4和行4后,我想要什么表更改为0:

    x1  x2  x3  x4  x5  x6
1   2   1   6   2   2   1
2   2   2   3   3   2   3
3   5   5   3   2   3   2
4   5   4   4   0   5   5
5   4   3   4   1   2   6
6   1   2   2   5   2   2

我试图让alghoritm找到到达目的地的最佳方式,重点是没有id,但总而言之,我想我还是需要添加它,因为在这些行上无法找到它…

当您在where中使用子查询时,您必须将其结果分配给某个对象。 您的查询应该如下所示

update game g1
set x4 = 0
where g1.id in
(select g2.id
  from game g2
 where g2.id in
       (select * from (select g3.id from game g3 order by g3.id desc) g4 where rownum <= 4)
 and rownum = 1);

您需要第一个顺序,然后才能使用rownum。

您需要一个“键”,即一个列[set],其值明确标识一条记录;如果找不到,最好重新访问数据模型和记录的排序标准

让我们假设您拥有这些,并通过将它们编码为假设的列id和sort1来简化事情

然后,您可以使用以下merge语句根据排序条件记录将前4名的x4列设置为0:

   MERGE INTO game g
        USING (
                 SELECT * FROM (SELECT * FROM game gs order by gs.sort1 desc) WHERE rownum <= 4
              ) src
           ON (
                 src.id = g.id
              )
 WHEN MATCHED
         THEN UPDATE SET g.x4 = 0
            ; 
演示


我想澄清一件事:你不需要ID,因为你需要一个键来进行更新,即使在你的表上有一个键会更好,但是你需要一些东西来对行进行排序,而不一定是键

换言之,假设您的表上有一个ID,PK基于该列,但该ID是一个随机值;这对于排序行和执行所需操作是无用的

您需要的是一些标准来对行进行排序,并确定哪一行是第四行

假设您的表中有一个创建日期,您可以使用该日期对更新的行和rowid进行排序

create table tabTest(createDate, x1,  x2,  x3,  x4,  x5,  x6) as (      
    select date '2018-01-01', 2 ,  1 ,  6 ,  2 ,  2 ,  1 from dual union all
    select date '2018-01-02', 2 ,  2 ,  3 ,  3 ,  2 ,  3 from dual union all
    select date '2018-01-03', 5 ,  5 ,  3 ,  2 ,  3 ,  2 from dual union all
    select date '2018-01-04', 5 ,  4 ,  4 ,  4 ,  5 ,  5 from dual union all
    select date '2018-01-05', 4 ,  3 ,  4 ,  1 ,  2 ,  6 from dual union all
    select date '2018-01-06', 1 ,  2 ,  2 ,  5 ,  2 ,  2 from dual
)
您可以使用合并和分析函数进行排序:

merge into tabTest t
using (select rowid, row_number() over (order by createDate) as RN from tabTest) t2
on (t2.RN = 4 and t.rowid = t2.rowid)
when matched then
    update set x4 = 0

同样,您可以根据任何列或列的组合对行进行排序,但您确实需要一个排序标准来确定哪一行是第四行。

请发布一些示例数据和所需结果。看看orderby和rownum,我相信使用select进行更新不是这里唯一的问题,不要使用rownum。另辟蹊径way@starko但问题是我需要使用rownum,因为我看不到其他手动记录的方法。@Aleksej我编辑了文章,并添加了数据的外观。现在的问题是:如何确定id=4的行是第四行?那就是:行是如何排序的?考虑表中的行没有顺序,因此必须明确排序规则。您确信RoNum和Orn通过这样的方式给出有用的东西吗?是的,它需要一点点修复。现在,它正在工作。在同一查询中使用rownum和orderby有助于选择最佳结果。关键是我编辑的帖子中的表中并没有id列,所以我知道表是什么样子的。
       UPDATE game g
          SET g.x4 = 0
        WHERE g.id = (
                  SELECT id FROM (SELECT * FROM game gs order by gs.sort1 desc) WHERE rownum = 4
              )
            ; 
create table tabTest(createDate, x1,  x2,  x3,  x4,  x5,  x6) as (      
    select date '2018-01-01', 2 ,  1 ,  6 ,  2 ,  2 ,  1 from dual union all
    select date '2018-01-02', 2 ,  2 ,  3 ,  3 ,  2 ,  3 from dual union all
    select date '2018-01-03', 5 ,  5 ,  3 ,  2 ,  3 ,  2 from dual union all
    select date '2018-01-04', 5 ,  4 ,  4 ,  4 ,  5 ,  5 from dual union all
    select date '2018-01-05', 4 ,  3 ,  4 ,  1 ,  2 ,  6 from dual union all
    select date '2018-01-06', 1 ,  2 ,  2 ,  5 ,  2 ,  2 from dual
)
merge into tabTest t
using (select rowid, row_number() over (order by createDate) as RN from tabTest) t2
on (t2.RN = 4 and t.rowid = t2.rowid)
when matched then
    update set x4 = 0