dosf40815 2017-04-29 09:19 采纳率: 100%
浏览 46
已采纳

如何将附加列和值添加到MySQL查询INSERT SELECT

Hi I have 2 columns tbl_feestudent and tbl_feeschool and this is their columns

feestudent_id, student_id, schoolyear_id, gradelevel_id, feetype_id, and feestudent_amount.

feeschool_id, schoolyear_id, gradelevel_id, feetype_id, and feeschool_amount.

I'm using a MySQL Query of INSERT SELECT where all the items that selected in tbl_feeschool will be inserted in tbl_feestudent

tbl_feestudent however has additional column which is student_id

How would I insert a value of student_id to all inserted values coming from tbl_feeschool

Let's just say the value of student_id is 40, the table would like like this.

enter image description here

As of now this is my query of INSERT SELECT with WHERE schoolyear_id = 4 and gradelevel_id = 1.

INSERT INTO tbl_feestudent (schoolyear_id, gradelevel_id, feetype_id, feestudent_amount) 
SELECT schoolyear_id, gradelevel_id, feetype_id, feeschool_amount 
FROM tbl_feeschool 
WHERE schoolyear_id = 4 AND gradelevel_id = 1
  • 写回答

1条回答 默认 最新

  • dongmubei7950 2017-04-29 09:25
    关注

    Add id to your select query suppose your student_id is 40 then

    INSERT INTO tbl_feestudent (student_id,schoolyear_id, gradelevel_id, feetype_id, feestudent_amount) 
    SELECT 40,schoolyear_id, gradelevel_id, feetype_id, feeschool_amount 
    FROM tbl_feeschool 
    WHERE schoolyear_id = 4 AND gradelevel_id = 1
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端