2017-09-04 38 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);