简体   繁体   English

什么是正确的SQL语法?

[英]What is the correct SQL Syntax?

I am struggling to resolve a SQL syntax problem in a Laravel Eloquent query. 我正在努力解决Laravel Eloquent查询中的SQL语法问题。 So I've gone right back to the root, and tried to get a query working in simple SQL. 因此,我回到了根源,并试图使查询在简单的SQL中工作。 And I'm stumped. 而且我很沮丧。 I'm no expert, but this is baffling me. 我不是专家,但这让我感到困惑。

I've had a look at the Update with Join in SQLite post, but I think that one is more around the issue of joins. 我看过《 在SQLite中使用Join更新》一文 ,但我认为其中更多的是关于join的问题。

I'm testing this on SQLLite, which surely is representative of SQL proper? 我正在SQLLite上对此进行测试,这肯定可以代表SQL吗?

Two tables: Instructions and Knowns. 两张表:说明和已知。 I want to update one column in the Knowns from (the latest) Instructions. 我想更新(最新)说明中的已知内容中的一列。 Get that right, and I can solve the rest myself (I hope!). 正确处理,剩下的我自己解决(我希望!)。

-- This works fine
Select instructions.rowid from instructions 
where instructions.EngagementTitle not null


-- This doesn't
UPDATE knowns 
SET    EngagementTitle = instructions.EngagementTitle
WHERE  id IN (
  SELECT knowns.id 
  FROM   knowns 
  INNER JOIN instructions 
  ON knowns.reference = instructions.reference
) 

Error Message 错误信息

no such column: instructions.EngagementTitle: 

UPDATE knowns 
SET    EngagementTitle = instructions.EngagementTitle 
WHERE  id IN (
  SELECT knowns.id 
  FROM   knowns 
  LEFT JOIN instructions 
  ON knowns.reference = instructions.reference
) 

Both tables both have the column - triple checked. 两个表都具有列-三重选中。

`EngagementTitle` varchar NOT NULL

What am I missing? 我想念什么?

UPDATE knowns 
SET    EngagementTitle = instructions.EngagementTitle 
...

The instructions table isn't "known" here, only knowns . instructions表不“知”在这里,只有knowns The scope of the table expressions is "downwards" only. 表表达式的范围仅是“向下”。 You can use a table introduced in a superquery in a subquery but not the other way round. 您可以在子查询的超级查询中使用引入的表,但反之则不能。

Try using subselects: 尝试使用子选择:

UPDATE knowns
       SET engagementtitle = (SELECT instructions.engagementtitle
                                     FROM instructions
                                     WHERE instructions.reference = knowns.reference)
       WHERE EXISTS (SELECT *
                            FROM instructions 
                            WHERE instructions.reference = knowns.reference);

I also replaced your WHERE with an EXISTS . 我还用EXISTS替换了您的WHERE I guess that is how you actually wanted it. 我想这就是您实际想要的方式。 It will only update rows from knowns where an entry exists in instructions for. 它只会从更新行knowns如果一个条目中存在instructions进行。 The way you did it, with a LEFT JOIN in the subquery to IN , you'd just updated all rows, as a LEFT JOIN includes all rows from the left table, thus all IDs from knowns are in the result (You maybe wanted an INNER JOIN , that would have worked.). 在子查询中使用LEFT JOININ ,您刚刚更新了所有行,因为LEFT JOIN包括左表中的所有行,因此,所有knowns ID都在结果中(您可能想要一个INNER JOIN ,那会起作用的。)。

But note, there must no more than one entry in instructions for one row in knowns for this to work. 但要注意,必须有不超过一个条目instructions一行中knowns这个工作。 I silently assumed that this is the case. 我默默地假设情况就是这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM