MySQL,将AUTO_INCREMENT传递给另一个表 [英] MySQL, passing a AUTO_INCREMENT to another table

查看:39
本文介绍了MySQL,将AUTO_INCREMENT传递给另一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于另一个人的回答正确,并且我有了解决我的问题的新思路(这与下面的情况并不完全一样),所以我启动了一个新线程.

Since the other one was answered correctly and i got a new idea for solving my problem (that's not really like the case under) i start a new thread.

同一示例,cds.id是AUTO_INCREMENT,这两个表实际上没有任何关系,是否有任何方法可以将生成的id发布到语言表中?

Same example, the cds.id is AUTO_INCREMENT and these two tables does not really have any relations, is there any way to post the generated id to the languages table?

Table for CDs (cds):
id | type
-----------------------
1  | CD  
2  | LP  
3  | CD  

Table for names:
cd_id | language | name
-----------------------
1     | fi       | AAA
1     | de       | AAACHTUNG
3     | en       | CCC

现在SELECT确实非常简单,结构也更好, 如果我现在有用于创建CD的电子表格,例如

Now the SELECT would be really simlple and the structure is better, If i now have e form for creating a CD, like

<input... > = the name -> 'best of XX'
<select...> = language -> 'en'
<select...> = type     -> 'CD' 

我要发表帖子,如何进行多次插入? 这些表的插入应该是...

and i do a post, how can i do a multiple insert? The insert to these tables should be...

INSERT INTO cds ('', 'CD');
INSERT INTO languages ('*the generated id in table above*', 'en', 'best of XX');

推荐答案

MySQL具有

MySQL has LAST_INSERT_ID() which returns the AUTO_INCREMENT value for the most recent INSERT.

INSERT INTO languages (LAST_INSERT_ID(), 'en', 'best of XX');

大多数语言都有此功能的包装器.例如,在PHP中,它是 mysqli_insert_id() .

Most languages have a wrapper for this function. For example, in PHP it's mysqli_insert_id().

这篇关于MySQL,将AUTO_INCREMENT传递给另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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