【问题标题】:Is there a way i can do multiple inserts into one table using a condition?有没有一种方法可以使用条件在一个表中进行多次插入?
【发布时间】:2010-01-30 17:40:28
【问题描述】:

有没有一种方法可以使用条件在一个表中进行多次插入?

我在 tbl_subscribers 中有一个订阅者列表。我有关于 productX 的更新,所以我希望订阅 productX 的每个人都能收到通知。 user_notification 表是id PK, user_id, notification_id。我需要的两个值是 product_id (productX),它允许我在 tbl_subscribers 和 notification_id 中找到订阅者列表以插入到 user_notification 表中。

如何使用一个查询进行此插入?我看到你可以在 sqlite http://www.sqlite.org/lang_insert.html 中做一个选择语句,但我无法理解我是如何做到这一点的,也没有看到一个例子。

【问题讨论】:

    标签: sql sqlite


    【解决方案1】:

    我相信您正在从此处概述的 INSERT SELECT 中查找:

    http://www.1keydata.com/sql/sqlinsert.html

    第二种类型的 INSERT INTO 允许 我们将多行插入到 桌子。与前面的例子不同, 我们在其中插入一行 为所有列指定其值, 我们现在使用 SELECT 语句 指定我们想要的数据 插入表中。如果你是 思考这是否意味着你 正在使用来自他人的信息 表,你是对的。语法是 如下:

    INSERT INTO "table1" ("column1", "column2", ...) 
    SELECT "column3", "column4", ... FROM "table2"
    

    【讨论】:

      【解决方案2】:
      insert into user_notification(user_id, notification_id)
      select s.user_id, @notification_id 
      from tbl_subscriber s 
      where s.product_id = @productX
      

      【讨论】:

        猜你喜欢
        • 2022-06-11
        • 2022-01-18
        • 1970-01-01
        • 2023-04-01
        • 2013-06-22
        • 2017-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多