使用另一个表中的值更新表

发布于 2024-09-28 11:32:56 字数 817 浏览 4 评论 0原文

我有这些表:

customer:
    customer_id vat_number
    =========== ==========
              1 ES-0000001
              2 ES-0000002
              3 ES-0000003


invoice:
    invoice_id customer_id vat_number
    ========== =========== ==========
           100           1 NULL
           101           3 NULL
           102           3 NULL
           103           2 NULL
           104           3 NULL
           105           1 NULL

我想用 customer.vat_number 中的当前值填充 invoice.vat_number 中的 NULL 值。是否可以用一条 SQL 语句来完成?

到目前为止我所拥有的触发了语法错误:

UPDATE invoice
SET vat_number=cu.vat_number /* Syntax error around here */
FROM invoice iv
INNER JOIN customer cu ON iv.customer_id=cu.customer_id
WHERE invoice.invoice_id=iv.invoice_id;

I have these tables:

customer:
    customer_id vat_number
    =========== ==========
              1 ES-0000001
              2 ES-0000002
              3 ES-0000003


invoice:
    invoice_id customer_id vat_number
    ========== =========== ==========
           100           1 NULL
           101           3 NULL
           102           3 NULL
           103           2 NULL
           104           3 NULL
           105           1 NULL

I want to fill the NULL values at invoice.vat_number with the current values from customer.vat_number. Is it possible to do it with a single SQL statement?

What I have so far triggers a syntax error:

UPDATE invoice
SET vat_number=cu.vat_number /* Syntax error around here */
FROM invoice iv
INNER JOIN customer cu ON iv.customer_id=cu.customer_id
WHERE invoice.invoice_id=iv.invoice_id;

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

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

发布评论

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

评论(4

不必在意 2024-10-05 11:32:56

使用 MySQL,ANSI-92 JOIN 语法:

UPDATE INVOICE
  JOIN CUSTOMER ON CUSTOMER.customer_id = INVOICE.customer_id
   SET vat_number = CUSTOMER.vat_number  
 WHERE INVOICE.vat_number IS NULL

使用 MySQL,ANSI-89 JOIN 语法:

UPDATE INVOICE, CUSTOMER 
   SET INVOICE.vat_number = CUSTOMER.vat_number  
 WHERE CUSTOMER.customer_id = INVOICE.customer_id
   AND INVOICE.vat_number IS NULL

对于 更多信息,请参阅 MySQL UPDATE 文档。这是 MySQL 特定的 UPDATE 语句语法,其他数据库不太可能支持。

Using MySQL, ANSI-92 JOIN syntax:

UPDATE INVOICE
  JOIN CUSTOMER ON CUSTOMER.customer_id = INVOICE.customer_id
   SET vat_number = CUSTOMER.vat_number  
 WHERE INVOICE.vat_number IS NULL

Using MySQL, ANSI-89 JOIN syntax:

UPDATE INVOICE, CUSTOMER 
   SET INVOICE.vat_number = CUSTOMER.vat_number  
 WHERE CUSTOMER.customer_id = INVOICE.customer_id
   AND INVOICE.vat_number IS NULL

For more info, see the MySQL UPDATE documentation. This is MySQL specific UPDATE statement syntax, not likely to be supported on other databases.

秋意浓 2024-10-05 11:32:56
UPDATE invoice i, customer cu SET i.vat_number=cu.vat_number 
WHERE i.customer_id = cu.customer_id;

在这里


SET vat_number=cu.vat_number /* 这里有语法错误 */
该错误是因为 var_number 列名称不明确 - MySQL 不知道这是 i.vat_number 还是 cu,vat_number。

UPDATE invoice i, customer cu SET i.vat_number=cu.vat_number 
WHERE i.customer_id = cu.customer_id;

Here you go


SET vat_number=cu.vat_number /* Syntax error around here */
The error is because the var_number column name is ambiguous - MySQL does not know if this is i.vat_number or cu,vat_number.

怼怹恏 2024-10-05 11:32:56

像这样的东西:

UPDATE invoice in
SET vat_number=(SELECT cu.vat_number FROM customer cu 
WHERE in.customer_id=cu.customer_id)
-- not tested

Something like :

UPDATE invoice in
SET vat_number=(SELECT cu.vat_number FROM customer cu 
WHERE in.customer_id=cu.customer_id)
-- not tested
乖乖哒 2024-10-05 11:32:56
UPDATE iv
SET iv.vat_number=cu.vat_number
FROM invoice iv
INNER JOIN customer cu ON iv.customer_id=cu.customer_id
UPDATE iv
SET iv.vat_number=cu.vat_number
FROM invoice iv
INNER JOIN customer cu ON iv.customer_id=cu.customer_id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文