Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
985 views
in Technique[技术] by (71.8m points)

sql - MySQL, passing a AUTO_INCREMENT to another table

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.

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

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');
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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');

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...