我如何将一列设置为MySQL为先前设置的行生成的数字?

问题描述 投票:-1回答:1

因此,我在MySQL上创建了INSERT语句,该语句向表中添加了特定的行。 invoice_id已设置为DEFAULT。现在,我需要找出继续自动生成的ID。我该怎么办?

我已经尝试使用DEFAULT和AUTO来查看它们是否可以解决问题。他们没有。

    INSERT into ap.terms 
VALUES (terms_id = 6, terms_description = 'Net due 120 days', terms_due_days = 120);

UPDATE ap.terms 
SET terms_description = 'Net due 125 days', terms_due_days = 125
WHERE terms_id = 6;

DELETE FROM ap.terms
WHERE terms_id = 6;

INSERT into ap.invoices
VALUES (AUTO_INCREMENT, 32, 'AX-014-027', '2014-08-01', '434.58', '0.00', '0.00', 2, '2014-08-31', NULL);

INSERT into ap.invoice_line_items
VALUES (invoice_sequence = 1, account_number = 160, line_item_amount = '180.23', line_item_description = 'Hard drive'), (invoice_sequence = 2, account_number = 527, line_item_amount = '254.35', line_item_description = 'Exchange Server update')
SET invoice_id = last_insert_id(1,1);

UPDATE ap.invoices
SET credit_total = invoice_total*0.1, invoice_total = (payment_total + credit_total)
WHERE invoice_number = 'AX-014-027';  

UPDATE ap.vendors
SET default_account_number = 403
WHERE vendor_id = 44;

UPDATE ap.invoices
SET terms_id = 2
WHERE default_terms_id = 2;

DELETE FROM ap.invoice_line_items
WHERE invoice_id = last_insert_id(1,1);

DELETE FROM ap.invoices
WHERE invoice_id = last_insert_id(1,1);  

因此,在第16行显示“ SET invoice_id = last_insert_id(1,1)”是我的错误所在。我该如何解决?

错误代码1062,您的sql语法错误。检查相应的手册。

mysql sql-insert sql-delete
1个回答
0
投票

您的表结构不正确。您的主键应设置为自动递增,默认为无。您应该使用LAST_INSERT_ID()检索插入到数据库中的最后一个行ID。

© www.soinside.com 2019 - 2024. All rights reserved.