嗨,我是mysql的新手,我可以成功插入一行,但是我想插入三行,但是我一直在花很多时间思考,如果有人可以提供建议或帮助我,我将非常感谢您
INSERT INTO earning (id, dateCreated,
dateModified,
x_t_name,
x_start_date,
x_end_month,
x_address,x_category,x_pic,x_promo_space,x_start_month,x_space_query,x_organizer,x_end_date
,x_address_query,x_current_pic,x_start_year
,x_end_year
,x_event_title
,x_pix_name),
SELECT x_add1,dateCreated,dateModified,"","2018-11-01",
x_end_month,"address1","",x_current_pic,"",x_start_month,"''","", "2018-11-12","'address1'"
,x_current_pic,x_end_year,x_end_year,"",""
FROM Sale
WHERE id = 'x1'
但是随后我想在多行中插入我要分配不同的ID和地址
我尝试过这样,但是我知道它错了
INSERT INTO earning (id, dateCreated,
dateModified,
x_t_name,
x_start_date,
x_end_month,
x_address,x_category,x_pic,x_promo_space,x_start_month,x_space_query,x_organizer,x_end_date
,x_address_query,x_current_pic,x_start_year
,x_end_year
,x_event_title
,x_pix_name),
(id, dateCreated,
dateModified,
x_t_name,
x_start_date,
x_end_month,
x_address,x_category,x_pic,x_promo_space,x_start_month,x_space_query,x_organizer,x_end_date
,x_address_query,x_current_pic,x_start_year
,x_end_year
,x_event_title
,x_pix_name),
(id, dateCreated,
dateModified,
x_t_name,
x_start_date,
x_end_month,
x_address,x_category,x_pic,x_promo_space,x_start_month,x_space_query,x_organizer,x_end_date
,x_address_query,x_current_pic,x_start_year
,x_end_year
,x_event_title
,x_pix_name)
SELECT x_add1,dateCreated,dateModified,"","2018-11-01",
x_end_month,"address1","",x_current_pic,"",x_start_month,"''","", "2018-11-12","'address1'"
,x_current_pic,x_end_year,x_end_year,"",""
FROM Sale
WHERE id = 'x1'
SELECT x_add2,dateCreated,dateModified,"","2018-11-01",
x_end_month,"address2","",x_current_pic,"",x_start_month,"''","", "2018-11-12","'address2'"
,x_current_pic,x_end_year,x_end_year,"",""
FROM Sale
WHERE id = 'x1'
SELECT x_add13,dateCreated,dateModified,"","2018-11-01",
x_end_month,"address3","",x_current_pic,"",x_start_month,"''","", "2018-11-12","'address3'"
,x_current_pic,x_end_year,x_end_year,"",""
FROM Sale
WHERE id = 'x1'
答案 0 :(得分:2)
要插入多行,请使用以下语法:
//Check if courses_enrolled_module_id is set
$courses_enrolled_module_id = $this->request->getQuery('courses_enrolled_module_id');
//If so make sure it is valid
if($courses_enrolled_module_id){
$this->loadModel('CoursesEnrolledModules');
$coursesEnrolledModule = $this->CoursesEnrolledModules->get($courses_enrolled_module_id,
['contain'=>[],
]);
//Pass variable to view so we can show correct back button
$this->set('coursesEnrolledModule', $coursesEnrolledModule);
//Also after save we will redirect.
}
答案 1 :(得分:1)
正确的语法是
INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;
您可以尝试在where子句中添加另一个语句,如下所示
WHERE id = 'x1' and x_address IN (address1, address2, address3)
答案 2 :(得分:1)
您可以在UNION
查询中使用SELECT
插入所有值。请注意,UNION
的默认行为是删除重复的行,因此,如果要将它们放在表中,则需要将UNION
更改为UNION ALL
。
INSERT INTO earning (id, dateCreated,
dateModified,
x_t_name,
x_start_date,
x_end_month,
x_address,x_category,x_pic,x_promo_space,x_start_month,x_space_query,x_organizer,x_end_date
,x_address_query,x_current_pic,x_start_year
,x_end_year
,x_event_title
,x_pix_name)
SELECT x_add1,dateCreated,dateModified,"","2018-11-01",
x_end_month,"address1","",x_current_pic,"",x_start_month,"''","", "2018-11-12","'address1'"
,x_current_pic,x_end_year,x_end_year,"",""
FROM Sale
WHERE id = 'x1'
UNION
SELECT x_add2,dateCreated,dateModified,"","2018-11-01",
x_end_month,"address2","",x_current_pic,"",x_start_month,"''","", "2018-11-12","'address2'"
,x_current_pic,x_end_year,x_end_year,"",""
FROM Sale
WHERE id = 'x1'
UNION
SELECT x_add13,dateCreated,dateModified,"","2018-11-01",
x_end_month,"address3","",x_current_pic,"",x_start_month,"''","", "2018-11-12","'address3'"
,x_current_pic,x_end_year,x_end_year,"",""
FROM Sale
WHERE id = 'x1'