如何在MySQL查询中的同一个表上同时执行插入和选择操作?

时间:2013-12-26 17:29:39

标签: php mysql sql bulkinsert insert-select

我的数据库中有一个名为“问题”的表格。为了您的参考,我在下面指定表'问题'的结构:

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'])的前两个值提取的问题添加到同一个表中。但是所有这些新插入的问题都应该在下一个评论(i.e. $_POST['to_subject_id'] and $_POST['to_topic_id'])之后给出subject_id和topic_id值。 总之,我想在同一个表上同时执行Insert和select操作。为了达到这个目的,我尝试了许多技巧以及谷歌搜索解决方案,但无法找到完美的解决方案。有人可以帮我这方面吗?我尝试使用以下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'

非常感谢你

2 个答案:

答案 0 :(得分:4)

将您想要的值替换为查询的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 :(得分:0)

我不确定我理解你的问题。您想要将新记录添加到同一个表中,但是将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。