如何编写条件选择插入语句以从表中获取记录,并基于某些特定列的值

时间:2017-06-06 18:33:58

标签: sql oracle

例如:

表1:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>Clothing mixer</h1>
<div class="container">
  <div id="avatar_box">
    <div id="base"><img src="https://i.imgur.com/FBCF5Mq.png"></div>
  </div>
  <div id="roulette">
    <div id="categoryBar">
      <div class="categoryButton ThisOneIsActive">
        Hair
      </div>
      <div class="categoryButton">
        Skin
      </div>
      <div class="categoryButton">
        Pants
      </div>
      <div class="categoryButton">
        Skirt
      </div>
      <div class="categoryButton" id="youreWearing">
        Wearing
      </div>
    </div>
    <div id="wardrobe_content">
      <div class="categoryContent ThisOneIsActive" id="c0">
        <div class="window hair"><img class="category hair" id="item_id_1" src="https://i.imgur.com/k0TXwzN.png"></div>
        <div class="window hair"><img class="category hair" id="item_id_2" src="https://i.imgur.com/ZdrNHKy.png"></div>
      </div>
      <div class="categoryContent" id="c1">
        <div class="window skin"><img class="category skin" id="item_id_3" src="https://i.imgur.com/lf8WdTw.png"></div>
      </div>
      <div class="categoryContent" id="c2">
        boxes of items pants
      </div>
      <div class="categoryContent" id="c3">
        boxes of items skirts
      </div>
    </div>
    <div class="items_on_doll" id="c4">
      &nbsp;
    </div>
  </div>
</div>

表2

c1,c2,c3 (1,10,'123')

现在我想从table1中选择记录,如果c3列值开始两位数是12那么我必须填充AA,如果是34那么表2中的BB c4列:

  • 表1 - (1,10,&#39; 123&#39;)
  • 表2 - (1,10,&#39; 123&#39;,&#39; AA&#39;)
  • 表1 - (1,10,&#39; 3444&#39;)
  • 表2 - (1,10,&#39; 3444&#39;,&#39; BB&#39;)

1 个答案:

答案 0 :(得分:0)

OP已在原帖中的评论中提供了其他信息。特别是,c3VARCHAR2,始终以1234开头。

以下内容应该有效。如果c3不以1234开头,则c4列中插入的值将为NULL。这是CASE表达式的默认行为,因此不需要明确编码;如果在这种情况下需要类似'ZZ'的内容,那么我们可以向else添加CASE子句。 (OP说c3始终以1234开头;如果是,则这是一个没有实际意义的点。)

insert into table2 ( c1, c2, c3, c4 )
  select c1, c2, c3,
         case when c3 like '12%' then 'AA'
              when c3 like '34%' then 'BB'
         end
  from   table1
;

<强>演示

SQL> create table table1 ( c1 number, c2 number, c3 varchar2(25) );
Table created.

SQL> insert into table1 ( c1, c2, c3 ) values ( 1, 10, '123' );
1 row created.

SQL> insert into table1 ( c1, c2, c3 ) values ( 1, 10, '3444' );
1 row created.

SQL> commit;
Commit complete.

SQL> select * from table1;

        C1         C2 C3
---------- ---------- -------------------------
         1         10 123
         1         10 3444

2 rows selected.

然后:

SQL> create table table2 ( c1 number, c2 number, c3 varchar2(25), c4 varchar2(10) );
Table created.

SQL> select * from table2;
no rows selected

SQL> insert into table2 ( c1, c2, c3, c4 )
  2    select c1, c2, c3,
  3           case when c3 like '12%' then 'AA'
  4                when c3 like '34%' then 'BB'
  5           end
  6    from   table1
  7  ;

2 rows created.

SQL> select * from table2;

        C1         C2 C3                        C4
---------- ---------- ------------------------- ----------
         1         10 123                       AA
         1         10 3444                      BB

2 rows selected.