mysql - 如何将一列设置为 MySQL 为先前设置的行生成的数字?
问题描述
因此,我在 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 语法有错误。检查相应的手册。
解决方案
您的表结构不正确。您的主键应设置为自动递增,默认为无。您应该使用LAST_INSERT_ID()
来检索插入到数据库中的最后一行 ID。
推荐阅读
- python - 如何使用 QChart 动画更新 BarSeries 中的值?
- javascript - 画布drawImage img源与画布源
- python - Luigi Workflows 中的条件分支
- ios - Swift JSON 计时问题创建错误消息
- r - 如何使用 R SPREDA 包提取 Fréchet 拟合模型的参数 xi 值?
- r - R中的桑基图
- java - 在图像视图上添加拖动/拉伸文本贴纸
- c# - C#GetResponse() 如何使动态创建的 url 超时?
- .htaccess - 403 重定向不起作用,但 404 可以。怎么了,我该怎么办?
- javascript - 如何监听下一页已开始加载的事件?