这个mysql插入查询有什么问题?

时间:2011-02-08 11:34:49

标签: mysql union insert-update

insert into tblcustomermachine
(
 select * from
 ((select vch_CustomerID  from tblcustomer where tblcustomer.vch_CustomerID='Cust00001' )
 union all
 (select Rate  from tblmachine))  as t );

该表包含18个cols,此结果集还包含18行,但它显示“列计数与第1行的值计数不匹配”。为什么呢?

2 个答案:

答案 0 :(得分:1)

您的表tblcustomermachine看起来比1列多。

与Simone一样,将您的插入内容更新为INSERT INTO tblcustomermachine(col_1) SELECT ...

您可以在INSERT期间跳过列名称,但SELECT需要返回表格所包含的相同数量的列。

答案 1 :(得分:0)

AFAIK,您必须声明字段名称:

insert into tblcustomermachine (col_1, col_2, col_3, ... col_18) (
   select t.field1, t.field2, t.field3, ... t.field18 from (
      (select vch_CustomerID from tblcustomer where tblcustomer.vch_CustomerID='Cust00001') 
       union all (select Rate from tblmachine))
   as t
   );