2017-09-04 70 views
0

=的ID我曾尝试:创建表3 AS SELECT WHERE从表1从表2

CREATE TABLE temporary_bundle_wired 
AS SELECT * FROM temporary_bundle,wired_items 
WHERE temporary_bundle.id = wired_items.id; 

我有两个exisiting表:

  1. temporary_bundle
  2. wired_items

我想创建一个名为temporary_bundle_wired的第三个表。 在这个表我想从temporary_bundle WHERE temporary_bundle.id = wired_items.id

我也想从temporary_bundle删除这些记录,一旦我让他们搬进tempoary_bundle_wired插入所有行(和它们的列和字段)。

查询我已经试过回报:

duplicate column name Id

回答

1

查询我已经试过回报: duplicate column name Id

上述错误公然表示你有重复的列。新建表中的列名称(id)在两个旧表的公用属性(temporary_bundle.idwired_items.id)之间发生冲突

确保两个表之间没有其他公用属性。如果存在,则在插入之前将其从任一表中别名。

如果其他人不工作,试试这个。

CREATE TABLE temporary_bundle_wired 
AS 
SELECT 
t.id as id, user_id, base_item, extra_data, x, y, z, rot, wall_pos, limited_number, limited_stack, sandbox -- and all other attributes you want 
FROM temporary_bundle t, wired_items w 
WHERE t.id = w.id; 

删除 - 这是一个不同的查询在一起。

DELETE from temporary_bundle t WHERE t.id = (select id from wired_items); 
1

的问题是,两个表有一个名为ID列,您是创建新表选择两个。 您必须指定一个表中的每一列,所以你可以重命名id列:

CREATE TABLE temporary_bundle_wired 
AS SELECT a.id as 'othername', user_id, base_item, ..., b.* 
    FROM temporary_bundle a, wired_items b 
    WHERE temporary_bundle.id = wired_items.id; 

干杯。

+0

http://prntscr.com/gh1vfo。因此,对于查询中的每一列我会做... AS SELECT a.id as'temporary_bundle.id','user_id','room_id'等等? – buuencrypted

+0

是的。这是正确的 – sdsc81

+0

CREATE TABLE temporary_bundle_wired AS SELECT a.id as'id','user_id','base_item','extra_data','x','y','z','rot','wall_pos','有限数量','有限容量','沙箱'从temporary_bundle a,wired_items b WHERE a.id = b.id; ...这没有插入来自temporary_bundle的行,其中temporary_bundle.id = wired_items.id。它所做的只是这个http://prntscr.com/gh254q。我还需要填写temporary_bundle中的相应字段。 – buuencrypted

0

一个问题的确是id在两张表之间是相同的。其他列可能是好,但如果id是唯一的问题,然后使用using条款:

CREATE TABLE temporary_bundle_wired AS 
    SELECT * 
    FROM temporary_bundle JOIN 
     wired_items 
     USING (id);