如何使用“默认值列”创建一个新表,具体取决于我选择的表?

时间:2015-06-06 03:55:56

标签: mysql database insert create-table

让我们说,我有两张桌子,一张用于“销售”,另一张用于“股票”。

销售表将是这样的:

-------------------------
| location | item | qty |
------------------------
|    1     |  11  |  1  |
|    2     |  12  |  1  |
-------------------------

股票表看起来像这样:

-------------------------
| location | item | qty |
------------------------
|    1     |  11  |  90 |
|    2     |  12  |  70 |
-------------------------

我想在两个表中为新表中的项目“11”和“12”插入数据,并在名为“type”的新列中将“sales”和“stock”分开,如下所示:< / p>

---------------------------------
| type  | location | item | qty |
---------------------------------
| sales |    1     |  11  |  1  |
| sales |    2     |  12  |  1  |
| stock |    1     |  11  |  90 |
| stock |    2     |  12  |  70 |
---------------------------------

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

    insert into table3 (thetype,location,item,qty) select 'sales',location,item,qty 
from sales where item in (11,12)


    insert into table3 (thetype,location,item,qty) select 'stock',location,item,qty 
from stock where item in (11,12)