【问题标题】:How to perform insert and select operation simultaneously on a same table in MySQL query?如何在 MySQL 查询中对同一张表同时执行插入和选择操作?
【发布时间】:2013-12-26 17:29:38
【问题描述】:

我的数据库中有一个名为“questions”的表。供您参考,我在表'questions'的结构下方指定:

question_id                   bigint(12) AUTO_INCREMENT (Primary Key)
question_parent_id            bigint(12)
question_subject_id           smallint(11)
question_topic_id             int(11)
question_directions           text
question_text                 text
question_file                 varchar(100)
question_description          text
question_difficulty_type      tinyint(4)
question_has_sub_ques         enum('0', '1')
question_picked_individually  enum('no', 'yes')
question_appeared_count       bigint(12)
question_manual               enum('0', '1')
question_site_id              varchar(10)
question_created_staff_id     varchar(32)
question_added_date           bigint(12)
question_updated_staff_id     varchar(32)
question_updated_date         bigint(12)

此表包含数千个问题。 现在的情况是我从 PHP 表单中获取几个值,如下所示:

/*Following are the subject id and topic id from which I want to fetch the questions belonging to that subjet id and topic id */    
    $_POST['from_subject_id'] => 8
    $_POST['from_topic_id'] => 545
/*Following are the subject id and topic id to which I want to add the questions fetched in above query*/
    $_POST['to_subject_id'] => 8
    $_POST['to_topic_id'] => 547

所以我想将根据 subject_id 和 topic_id(i.e. based on $_POST['from_subject_id'] and $_POST['from_topic_id']) 的前两个值提取的问题添加到同一张表中。但是所有这些新插入的问题都应该在下一条评论之后给出 subject_id 和 topic_id 值(i.e. $_POST['to_subject_id'] and $_POST['to_topic_id'])。 简而言之,我想在同一张表上同时执行插入和选择操作。为了实现这一点,我尝试了很多技巧,并在谷歌上搜索了解决方案,但无法找到完美的解决方案。任何人都可以在这方面帮助我吗?我尝试使用以下 SQL 查询,但它使用他们已经拥有的主题和主题 id 值插入相同的问题。简而言之,问题不断重复,我不想要这样的结果。相反,我希望使用新的 subject_id 和新的 topic_id 插入相同的问题。 供您参考,我在 SQL 查询下方给出:

INSERT INTO questions (question_parent_id, question_subject_id, question_topic_id, question_directions, question_text, question_file, question_description, question_difficulty_type, question_has_sub_ques, question_picked_individually, question_manual, question_site_id, question_created_staff_id, question_added_date, question_appeared_count, question_updated_staff_id, question_updated_date)
SELECT question_parent_id, question_directions, question_text, question_file, question_description, question_difficulty_type, question_has_sub_ques, question_picked_individually, question_manual, question_site_id, question_created_staff_id, question_added_date, question_appeared_count, question_updated_staff_id, question_updated_date
FROM questions
WHERE question_subject_id='8' AND question_topic_id='545'

非常感谢你

【问题讨论】:

    标签: php mysql sql bulkinsert insert-select


    【解决方案1】:

    将您想要的值代入查询的select 部分:

    INSERT INTO questions (question_parent_id, question_subject_id, question_topic_id, question_directions, question_text, question_file, question_description, question_difficulty_type, question_has_sub_ques, question_picked_individually, question_manual, question_site_id, question_created_staff_id, question_added_date, question_appeared_count, question_updated_staff_id, question_updated_date)
        SELECT question_parent_id,  8, 547,
               question_directions, question_text, question_file,
               question_description, question_difficulty_type, question_has_sub_ques,
               question_picked_individually, question_manual, question_site_id,
               question_created_staff_id, question_added_date, question_appeared_count,
               question_updated_staff_id, question_updated_date
    FROM questions
    WHERE question_subject_id = '8' AND question_topic_id = '545';
    

    或者,select 可能会开始:

    select question_parent_id, $_POST['to_subject_id'], $_POST['to_topic_id']
    

    【讨论】:

    • +1 我经常使用这种模式将数据从一个父 ID 复制到另一个。
    【解决方案2】:

    我不确定我是否理解您的问题。您想在同一张表中添加新记录,但将 topic_id 从 545 更改为 547?如果这就是你想要的,那么以下将起作用:

    INSERT INTO questions 
    SELECT 
        question_id ,
        question_parent_id ,
        question_subject_id + 1 ,
        547 ,
        question_directions ,
        question_text   ,
        question_file   ,
        question_description    ,
        question_difficulty_type    ,
        question_has_sub_ques   ,
        question_picked_individually    ,
        question_appeared_count ,
        question_manual ,
        question_site_id    ,
        question_created_staff_id   ,
        question_added_date ,
        question_updated_staff_id   ,
        question_updated_date   
    FROM questions
    WHERE question_subject_id=8 AND question_topic_id=545;
    

    需要进一步澄清的一些问题: 新插入的 question_parent_id 和现有行的 question_id 之间是否存在某种关系?还是新行只是继承了 question_id 和 question_parent_id 的旧值?

    您说您想要一个“新”主题 ID,但您没有指定它应该是什么。我只是通过向现有行添加 1 来增加主题 ID。如果您想要其他东西,请指定。在您的 PHP 示例中,它似乎应该保持 8。在这种情况下,请从我上面的代码中删除 +1。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-12
      • 1970-01-01
      • 2013-11-29
      相关资源
      最近更新 更多