简体   繁体   English

全部插入子查询

[英]Insert into union all ,subquery

i want to insert all data in datashop where shopdata with single dataid for shopid , i have two table 我想将所有数据插入到具有shopid的单个dataid的shopdata的datashop中,我有两个表

1st data , 第一数据

 id 
 1
 2
 3
 4
 5

2nd datashop 第二个数据商店

 dataid   | shopid
 -----------------
    1     |    1 
    2     |    1 
    3     |    1 
    4     |    1 
    5     |    1 
    1     |    2 
    2     |    2 
    3     |    2

now i want insert all data in datashop with shopid 现在我想用shopid将所有数据插入datashop

i try using a query but is not work ... 我尝试使用查询,但不起作用...

 INSERT INTO datashop( dataid , shopid ) 
 (Select id
 from (Select id from data
 Union all
 Select dataid from datashop )Z
 group by id as dataid , 3 as shopid) 

this query is not run show multiple errors 此查询未运行显示多个错误

5 errors were found during analysis. 分析期间发现5个错误。

Unexpected token. 意外的标记。 (near "(" at position 49) (在位置49的“(”附近)

An expression was expected. 期望表达。 (near "(" at position 71) (在位置71处的“(”附近)

Unexpected token. 意外的标记。 (near "(" at position 71) (在位置71处的“(”附近)

This type of clause was previously parsed. 此类子句先前已被解析。 (near "Select" at position 72) (在位置72处的“选择”附近)

Unrecognized statement type. 无法识别的语句类型。 (near "all" at position 111) (在“所有”位置111附近)

This is the syntax you want: 这是您想要的语法:

insert into datashop(dataid, shopid) 
    Select id, 3 as shopid
    from (Select id from data
          Union all
          Select dataid from datashop
         ) Z
    group by id;

You had several small issues: 您有几个小问题:

  • You were listing columns after the query. 您正在查询后列出列。 This was probably the cause of the syntax problems. 这可能是语法问题的原因。
  • You had an alias in the group by clause (related to previous point). 您在group by子句中有一个别名(与上一点有关)。
  • Your select did not have the appropriate number of columns. select的列数不正确。
  • The parentheses around the select are unnecessary. select周围的括号是不必要的。

Also note: the query could be simplified to: 另请注意:查询可以简化为:

insert into datashop(dataid, shopid) 
    Select id, 3
    from data
    Union
    Select dataid, 3
    from datashop;

union removes duplicates. union删除重复项。

insert into datashop(dataid, shopid)  
Select id as dataid, 3 as shopid
  from from data
Union 
Select id as dataid, 4 as shopid
  from from data
Union 
Select id as dataid, 5 as shopid
  from from data

The specification isn't very clear. 规格不是很清楚。

It looks like you have a table, let's call it (for sake of example) "shop" And that table contains a single column "id" (integer type?) and there are five rows in the table, with values 1 thru 5. It looks like the "id" column may be the primary key of the table, or at least guaranteed to be unique and non-null. 看起来您有一个表,我们称它为“商店”(例如),该表包含单个列“ id”(整数类型?),并且该表中有5行,其值是1到5。看起来“ id”列可能是表的主键,或者至少保证是唯一且非null。

Then it looks like you have a second table which you want to populate with some rows. 然后,看起来您有第二个表,要在其中填充一些行。 That table contains two columns, one named id, the other named shopid. 该表包含两列,一列名为id,另一列名为shopid。

We're not given any idea of what this table represents in the model, so we're really just guessing. 我们不知道此表在模型中代表什么,因此我们只是在猜测。 It looks to me like you might be looking for a Cartesian product, every shop.id matched to every other shop.id, including rows where the two columns are equal. 在我看来,您可能正在寻找一种笛卡尔乘积,每个shop.id与每个其他shop.id都匹配,包括两列相等的行。

For the lack of another name, we'll refer to this second table as "shop_to_shop". 由于缺少其他名称,我们将第二个表称为“ shop_to_shop”。

Again, this is all just a guess... but maybe you are looking to populate the shop_to_shop table with the result from a query something like this: 再说一次,这只是一个猜测……但是也许您正在寻找用如下查询结果填充shop_to_shop表:

  SELECT r.id   AS id
       , s.id   AS shopid
    FROM shop r
   CROSS
    JOIN shot s
   ORDER
      BY r.id
       , s.id

If "id" is unique in "shop" (again, just a guess) there won't be any duplicate tuples in the result. 如果“ id”在“商店”中是唯一的(同样,只是一个猜测),结果中将不会有任何重复的元组。 If there are already some rows in the "shop_to_shop" table and we don't want to create any duplicates of rows that are already in the table, we can use a anti-join 如果“ shop_to_shop”表中已经有一些行,并且我们不想创建表中已经存在的行的任何重复项,则可以使用反联接

  SELECT r.id   AS id
       , s.id   AS shopid
    FROM shop r
   CROSS
    JOIN shot s
    LEFT
    JOIN shop_to_shop t
      ON t.id     = r.id
     AND t.shopid = s.id
   WHERE t.id IS NULL
   ORDER
      BY r.id
       , s.id

Once we get a SELECT statement that's returning the rows that we want to insert, whatever that is, convert that into an INSERT statement by preceding it with "insert tablename (colname,colname) " 一旦获得返回要插入的行的SELECT语句,无论是什么,都可以在其前面加上“插入表名(colname,colname)”,将其转换为INSERT语句。

  INSERT INTO shop_to_shop (id, shopid)
  SELECT ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM