2011-03-04 64 views
0

我有2個表。 表A柱 - 援助,aname 表B柱 - 競價,BNAME從表中插入一列數據到其他表中,但其他列數據將被動態指定

從表A,我會拿起從列「援助」的數據, 並在「出價」表B的列插入,但在表B的bname列中,我將插入一個新值。我該怎麼做呢?

create table A(aid int,aname char) 

insert into A values(111, 'e') 


create table B(bid int, bname char) 


insert into B (bid,bname) 

競價會從查詢值:選擇從 BNAME援助將得到一個新的價值-m

預期的結果應該是:表B將有:

投標BNAME --- ----- 111米

+0

你想在'bname'上插入什麼,預期的結果是什麼? – Lamak 2011-03-04 16:01:08

回答

2

嘗試這種情況:

insert into b (bid, bname) select aid, 'm' as bname_fixed_val from a 

兩個事實啓用了上述解決方案:

  1. insert .. select子句允許你插入值與任何select返回。
  2. 可以作爲字段返回常數值與select,例如像:

    SELECT 0 as id, 'John' as name 
    

結合這兩點在一起,我用了一個insert..select子句從第一個表中的字段值(aid ),以及第二個字段的常量值(m)。 AS bname_fixed_val子句只是一個字段別名,可以省略。

有關SQL的更多信息,請看這裏的鏈接:http://www8.silversand.net/techdoc/teachsql/index.htm,雖然使用Google搜索也不會傷害到它。

+0

非常感謝,先生,它的工作,我可以有一個簡單的解釋也對這個查詢,你建議 – sqlchild 2011-03-04 16:12:11

相關問題