Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何使用“创建新表”;默认值列";取决于我从中选择的表格?_Mysql_Database_Insert_Create Table - Fatal编程技术网

Mysql 如何使用“创建新表”;默认值列";取决于我从中选择的表格?

Mysql 如何使用“创建新表”;默认值列";取决于我从中选择的表格?,mysql,database,insert,create-table,Mysql,Database,Insert,Create Table,比如说,我有两张桌子,一张是“销售”桌,另一张是“库存”桌 销售表如下所示: ------------------------- | location | item | qty | ------------------------ | 1 | 11 | 1 | | 2 | 12 | 1 | ------------------------- ------------------------- | location | item | qty | ---

比如说,我有两张桌子,一张是“销售”桌,另一张是“库存”桌

销售表如下所示:

-------------------------
| location | item | qty |
------------------------
|    1     |  11  |  1  |
|    2     |  12  |  1  |
-------------------------
-------------------------
| location | item | qty |
------------------------
|    1     |  11  |  90 |
|    2     |  12  |  70 |
-------------------------
---------------------------------
| type  | location | item | qty |
---------------------------------
| sales |    1     |  11  |  1  |
| sales |    2     |  12  |  1  |
| stock |    1     |  11  |  90 |
| stock |    2     |  12  |  70 |
---------------------------------
库存表如下所示:

-------------------------
| location | item | qty |
------------------------
|    1     |  11  |  1  |
|    2     |  12  |  1  |
-------------------------
-------------------------
| location | item | qty |
------------------------
|    1     |  11  |  90 |
|    2     |  12  |  70 |
-------------------------
---------------------------------
| type  | location | item | qty |
---------------------------------
| sales |    1     |  11  |  1  |
| sales |    2     |  12  |  1  |
| stock |    1     |  11  |  90 |
| stock |    2     |  12  |  70 |
---------------------------------
我想在新表中插入项目“11”和“12”的两个表中的数据,并在名为“type”的新列中用“sales”和“stock”分隔它们,如下所示:

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

有什么想法吗?

伙计,真的吗?你可以发现这里有一个提示,两个insert stmts使用union@AsConfused,我的问题是添加默认的“sales”和“stock”,而不是union或insert。因此,第一列是一个问题。你怎么能解决这个问题?是的,@asconflued。是的。
    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)