简体   繁体   中英

How to Add Additional Column and Value to a MySQL Query 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.

在此处输入图片说明

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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