【问题标题】:Swap two columns values between two tables在两个表之间交换两列值
【发布时间】:2018-08-17 23:54:24
【问题描述】:

我有两个表 NAVEEN_T1 和 NAVEEN_T2 有 Id 和 Name 之类的列。

如何根据Id交换两个表的name列值?

SQL> SELECT * FROM NAVEEN_T1; 身份证名称 ---------- ---------- 1 戈尔迪 2 纳文 3 阿米特 SQL> SELECT * FROM NAVEEN_T2; 身份证名称 ---------- ---------- 1 冉冉 2 索姆 3 阿拜

我想要这样的输出:

SQL> SELECT * FROM NAVEEN_T1; 身份证名称 ---------- ---------- 1 冉冉 2 索姆 3 阿拜 从 NAVEEN_T2 中选择 *; 身份证名称 ---------- ---------- 1 戈尔迪 2 纳文 3 阿米特

提前致谢。

【问题讨论】:

    标签: mysql oracle10g


    【解决方案1】:

    现在,这可能过于简单了,但是,嘿 - 这就是您的示例数据所暗示的。这个想法是:交换表名,而不是数据。看看:

    SQL> create table naveen_t1 (id number, name varchar2(20));
    
    Table created.
    
    SQL> create table naveen_t2 (id number, name varchar2(20));
    
    Table created.
    
    SQL> insert all
      2    into naveen_t1 values (1, 'GOLDI')
      3    into naveen_t1 values (2, 'NAVEEN')
      4    into naveen_t1 values (3, 'AMIT')
      5    --
      6    into naveen_t2 values (1, 'RANJAN')
      7    into naveen_t2 values (2, 'SOM')
      8    into naveen_t2 values (3, 'ABHAY')
      9  select * from dual;
    
    6 rows created.
    
    SQL> select * From naveen_t1;
    
            ID NAME
    ---------- --------------------
             1 GOLDI
             2 NAVEEN
             3 AMIT
    
    SQL> select * From naveen_t2;
    
            ID NAME
    ---------- --------------------
             1 RANJAN
             2 SOM
             3 ABHAY
    

    诀窍来了:

    SQL> rename naveen_t2 to temp;
    
    Table renamed.
    
    SQL> rename naveen_t1 to naveen_t2;
    
    Table renamed.
    
    SQL> rename temp to naveen_t1;
    
    Table renamed.
    
    SQL> select * from naveen_t1;
    
            ID NAME
    ---------- --------------------
             1 RANJAN
             2 SOM
             3 ABHAY
    
    SQL> select * From naveen_t2;
    
            ID NAME
    ---------- --------------------
             1 GOLDI
             2 NAVEEN
             3 AMIT
    
    SQL>
    

    【讨论】:

      【解决方案2】:
      CREATE TABLE naveen_t1
          (`id` int, `name` varchar(6))
      ;
      
      INSERT INTO naveen_t1
          (`id`, `name`)
      VALUES
          (1, 'GOLDI'),
          (2, 'NAVEEN'),
          (3, 'AMIT')
      ;
      
      
      CREATE TABLE naveen_t2
          (`id` int, `name` varchar(6))
      ;
      
      INSERT INTO naveen_t2
          (`id`, `name`)
      VALUES
          (1, 'RANJAN'),
          (2, 'SOM'),
          (3, 'ABHAY')
      ;
      create  table naveen_t1_b 
      as 
      select * from naveen_t1;
      create  table naveen_t2_b 
      as 
      select * from naveen_t2;
      
      update naveen_t1 ta set  ta.name=(select tb.name
                                        from naveen_t2_b tb where tb.id=ta.id);
      update naveen_t2 ta set  ta.name=(select tb.name
                                        from naveen_t1_b tb where tb.id=ta.id);
      select * from naveen_t1;
      select * from naveen_t2;
      

      希望这能解决您的问题,我已经创建了备份表来捕获这些值。 您可以使用临时表而不是永久表

      在这里查看-http://sqlfiddle.com/#!9/fdc52e/2

      【讨论】:

        【解决方案3】:

        你可以试试这个

        update table1 t1
        set    t1.col1 = (select t2.col2
                          from   table2 t2
                          where  t2.id = t1.id
                          and    t1.col1 < t1.col2)
        

        这样的事情应该很容易做到。

        我看到的唯一棘手的问题是将 table2 中的行与 table1 中的行匹配。在我的示例中,我假设两个表共享一个唯一的“id”列,可以轻松匹配。用更合适的方式修改查询。

        【讨论】:

        • 我试过但实际上我想交换列值。我不想比较小于和大于的值。我也提到了输出。我想要输出。跨度>
        猜你喜欢
        • 1970-01-01
        • 2011-12-06
        • 1970-01-01
        • 2012-04-23
        • 1970-01-01
        • 2018-05-16
        • 2022-01-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多