【问题标题】:How to create a new table with "default value column" depending on the tables I select from?如何根据我选择的表创建具有“默认值列”的新表?
【发布时间】:2015-06-06 03:55:56
【问题描述】:

假设我有两张表,一张用于“销售”,另一张用于“库存”。

销售表是这样的:

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

库存表如下所示:

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

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

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

有什么想法吗?

【问题讨论】:

  • 老兄真的吗?你可以把它敲出来
  • 这里提示两个插入语句忽略使用联合
  • @AsConfused,我的问题是添加默认的“sales”和“stock”,而不是 union 或 insert。
  • 所以第一列有问题。你怎么解决这个问题?
  • 是的,@AsConfused。是的。

标签: mysql database insert create-table


【解决方案1】:
    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)

【讨论】:

    猜你喜欢
    • 2015-08-02
    • 1970-01-01
    • 1970-01-01
    • 2012-11-09
    • 2020-11-11
    • 2021-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多