MySQL insert语句(插入表(列)select语句)

发布于 2024-12-09 03:38:40 字数 548 浏览 0 评论 0原文

我试图将值插入到表的选定列中,但只插入 MySQL 语句的结果。 select 语句本身可以正常工作并返回结果。

当与插​​入语句结合使用时,它失败了,

error incorrect syntax near `dedspgoods`.`case number`.

谁能帮助我使用正确的语法?我的错误语法如下:

insert into despgoods_alldetails 
  (`case number`, weight, customer, transporttypename) 
values 
( select despgoods.`case number`
       , despgoods.weight
       , despgoods.customer
       , customers.transporttypename 
  from despgoods 
    inner join customers 
      on despgoods.customer = customers.customer )

I am trying to insert values into selected columns of table, but only insert the results of a MySQL statement. the select statement works correctly by itself and returns the results.

when combined with the insert statement it is fails with

error incorrect syntax near `dedspgoods`.`case number`.

Can anyone assist me with the correct syntax? my erronous syntax is as below:

insert into despgoods_alldetails 
  (`case number`, weight, customer, transporttypename) 
values 
( select despgoods.`case number`
       , despgoods.weight
       , despgoods.customer
       , customers.transporttypename 
  from despgoods 
    inner join customers 
      on despgoods.customer = customers.customer )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

晨曦÷微暖 2024-12-16 03:38:40

如果这是有效的 SELECT

select despgoods.`case number`
     , despgoods.weight
     , despgoods.customer
     , customers.transporttypename 
from despgoods 
  inner join customers 
    on despgoods.customer = customers.customer

那么请尝试此操作(请注意,如果您想要插入 SELECT 的结果集,则没有 VALUES 子句:

insert into despgoods_alldetails 
  (`case number`, weight, customer, transporttypename) 
select despgoods.`case number`
     , despgoods.weight
     , despgoods.customer
     , customers.transporttypename 
from despgoods 
  inner join customers 
    on despgoods.customer = customers.customer

If this is the SELECT that works:

select despgoods.`case number`
     , despgoods.weight
     , despgoods.customer
     , customers.transporttypename 
from despgoods 
  inner join customers 
    on despgoods.customer = customers.customer

Then try this (notice there is no VALUES clause if you want to insert the result set of a SELECT:

insert into despgoods_alldetails 
  (`case number`, weight, customer, transporttypename) 
select despgoods.`case number`
     , despgoods.weight
     , despgoods.customer
     , customers.transporttypename 
from despgoods 
  inner join customers 
    on despgoods.customer = customers.customer
鹿童谣 2024-12-16 03:38:40

正如@EdHeal所述,您需要将case number括在引号或反引号之间,因为列名称中有空格(事实上CASE是保留字实际上只是一个有趣的细节,但这并不是在这种情况下破坏查询的原因)。

insert into despgoods_alldetails (`case number`,weight,customer,transporttypename) 
values (
    select despgoods.`case number`, despgoods.weight, despgoods.customer, customers.transporttypename
    from despgoods 
    inner join customers on (despgoods.customer=customers.customer)
)

As stated by @EdHeal, you'll need to enclose case number between quotes or backticks as there is a space in the column name (the fact that CASE is a reserved word is actually only an interesting detail, but that's not what breaks the query in this case).

insert into despgoods_alldetails (`case number`,weight,customer,transporttypename) 
values (
    select despgoods.`case number`, despgoods.weight, despgoods.customer, customers.transporttypename
    from despgoods 
    inner join customers on (despgoods.customer=customers.customer)
)
謌踐踏愛綪 2024-12-16 03:38:40

case 是保留字。需要将“案件编号”放在引号中。

case is a reserved word. Need to put 'case number' in quotes.

雨巷深深 2024-12-16 03:38:40

这里它去

 INSERT INTO despgoods_alldetails(
                                  casenumber,
                                  weight,
                                  customer,
                                  transporttyplename)


 VALUES(SELECT desp.casenumber,
        desp.weight,
        despgoods.customer, 
        customers.transporttypename)

 FROM despgoods
 INNER JOIN customers on despgoods.customer = customers.customer

那里。那应该可以正常工作。请记住,不要用空格命名变量,因为这可能会变得非常棘手。我认为你的 case 编号有误,因为当 case 是 MySQL 中的函数时,它之间有一个空格。如果您还需要什么,请告诉我。

here it goes

 INSERT INTO despgoods_alldetails(
                                  casenumber,
                                  weight,
                                  customer,
                                  transporttyplename)


 VALUES(SELECT desp.casenumber,
        desp.weight,
        despgoods.customer, 
        customers.transporttypename)

 FROM despgoods
 INNER JOIN customers on despgoods.customer = customers.customer

there. that should work fine. Remember, do not name your variables with spaces, because it can get real tricky. I think you had an error in case number because it has a space in between, when case is a function in MySQL. Let me know if you need anything else.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文