使用子查询将新客户插入 table

Inserting new customer into table using subquery

插入客户 Jake Smith,其送货地址为“yknhl fr hluhw”,可以通过“1236548780”和“jakesmith@email.com”联系到他(使用子查询从 shipping_details table, 直接插入地址会被标记为不正确).

^作业题说明

use assignment;   
insert into customers(first_name,last_name,email,phoneNo)    
values ('Jake','Smith','jakesmith@email.com','1236548780');
where shipping_detail_id in (select shipping_details.address from shipping_details where address like 'yknhl fr hluhw');

我在第 4 行中做错了什么,所以我可以成功地将新客户添加到 table 中。我感到困惑,不知道从那里去哪里

一个 你可以在 ÌNSERT INTO

definition 中看到

没有WHERE子句,UPDATE有一个

但是你可以使用

use assignment;   
insert into customers(first_name,last_name,email,phoneNo,shipping_detail_id)    
SELECT('Jake','Smith','jakesmith@email.com','1236548780', shipping_detail_id)
 from shipping_details where address like 'yknhl fr hluhw'
 LIMIT 1;

use assignment;   
insert into customers(first_name,last_name,email,phoneNo,shipping_detail_id)    
VALUES ('Jake','Smith','jakesmith@email.com','1236548780', (SELECT shipping_detail_id
 from shipping_details where address like 'yknhl fr hluhw'
 LIMIT 1));

LIMIT 1 是为了防止该地址有多个 ID,那么您应该添加一个 ORDER BY 以获得正确的