SQL帮助| INSERT语句的选择列表包含的项目少于插入列表

时间:2017-06-01 15:23:43

标签: mysql

请参阅下面的查询,

insert into dbo.orderDetails(orderNo,clientId,productId,quantity)
values(' ee941422-5546-4d62-b5d6-60ecd13ca2b8 ')
select client_id,product_id,amount from dbo.cart
where client_id =' efc08f7c-fdfc-4712-9488-fc1c55acb95e ' ;

在这里我想要一个静态orderno,其余的应该来自​​一个表(dbo.cart)。当我执行我的查询时它会显示这个错误

There are more columns in the INSERT statement than values specified in the 
VALUES clause. The number of values in the VALUES clause must match the 
number of columns specified in the INSERT statement.

任何解决方案。

2 个答案:

答案 0 :(得分:2)

您不能同时拥有VALUESSELECT。如果要插入静态值,请将其放入SELECT列表。

insert into dbo.orderDetails(orderNo,clientId,productId,quantity)
select ' ee941422-5546-4d62-b5d6-60ecd13ca2b8 ', client_id,product_id,amount from dbo.cart
where client_id =' efc08f7c-fdfc-4712-9488-fc1c55acb95e ' ;

答案 1 :(得分:0)

我的问题已经解决,下面是对我有用的查询

INSERT INTO Exam_Trigger(问题,CAT_ID,TOPIC_ID,SCHEDULED_TYPE,UPDATE_USER,UPDATE_DT,CREATE_USER,CREATE_DT) select id ,'2','2','Weekly','',curdate(),'',curdate() from ( select GROUP_CONCAT(id SEPARATOR ',')as id from(select id from question_data order by rand () limit 4)as ls) as ps