MySQL INSERT 语句插入两个表,使用 AI 值从另一个表 [英] MySQL INSERT statement into two tables, using AI value from one in the other

查看:67
本文介绍了MySQL INSERT 语句插入两个表,使用 AI 值从另一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个数据库,需要一条语句一次将值插入到两个表中.我这样做的问题是,其中一个表需要一个值,该值在另一个表中自动递增.我怎样才能做到这一点?不幸的是,我的主机不支持使用外键.(我使用的是 mySQL)
我已经把表名和值放在下面了.

I am designing a database, and need a statement inserting values into two tables at a time. The issue for me doing so, is the fact that one of the tables will need a value, which is autoincremented in the other. How can I do this? My host does not support the use of foreign keys unfortunately. (I am using mySQL)
I've put the table names and values below.

Task

+ id (auto incremented value)
+ dataid (should have been foreign key to data.id- which will be auto incremented when +tatement is xecuted)
+ priority (task priority 1-100)
+ statusid (should have been foreign key to status.id - this value is given)
+ toc (TimeOfCreation - timestamp, will be set automatically)

Data

+ id (auto incremented value)
+ text (the text of the task/privatemsg/statusmsg etc.)

你们中的一个或某人可以帮助我制作一个插入语句,使我可以将这些数据插入这些表中,其中 data.id 是自动递增的,并存储在 task.dataid 中吗?

Can one or someone of you help me make an insert statment making it possible for me to insert this data into these tables, where data.id is auto incremented, and stored in task.dataid?

text=fix login  
priority=99  
statusid=4

如果您有任何问题,请询问.
提前致谢

Please ask if you have any questions.
Thanks in advance

Pyracell

推荐答案

看到这个:

http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html

如果将一条记录插入包含 AUTO_INCREMENT 列的表中,则可以通过调用 mysql_insert_id() 函数获取存储在该列中的值."

"If you insert a record into a table that contains an AUTO_INCREMENT column, you can obtain the value stored into that column by calling the mysql_insert_id() function."

这篇关于MySQL INSERT 语句插入两个表,使用 AI 值从另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆