0

I have tricky SQL query to create, which if possible will save me a lot of PHP code.

So I have an HTML form where a visitor can create a user account with the fields name, email, password, interests.

For the name, email and password I have a simple INSERT query to enter this info into the database users, however the interests field can have multiple entries so this data is stored in many-to-many join tables (interests, user_interests).

In order for me enter these interests in the table user_interests, I need to obtain the User ID number which was auto-incremented from the initial INSERT query plus loop over another INSERT for each "interest" the user has chosen. Can I accomplish this all in one SQL query?

2
  • You'll have to use 2 queries or a transaction. The LAST_INSERT_ID() will be useful: dev.mysql.com/doc/refman/5.1/en/… Commented Oct 22, 2011 at 16:49
  • 1
    Probably yes, using INSERT INTO (cols. . .) SELECT cols. . . FROM . . ., but you'll need to state what information is in what tables when you want to execute this SQL. Commented Oct 22, 2011 at 16:49

2 Answers 2

1

If you want to do this with one command, then it sounds like a good case to learn how to write MySql stored procedures. The result will be much faster too.

http://dev.mysql.com/doc/refman/5.0/en/stored-routines-last-insert-id.html

http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-compound-statements.html

Sign up to request clarification or add additional context in comments.

3 Comments

And how do you intend to pass a list of ID's to the stored procedure? Perhaps I'm missing something, but I can't find any array-like datatypes in the MySQL manual.
I would pass it as a comma separated list, then parse out the data and build a single insert statement with multiple rows. There are definitely MySql functions to easily do that. Learning how to write SP is one of the most valuable skills in programming, because they are universal across all languages (you just call them with parameters)
1

Well, in MySQL an INSERT statement only inserts into one table. So no, it's impossible to accomplish this with a single query. But you can do it with two queries. MySQL supports a syntax where you can insert multiple rows with a single query:

insert into user_interests (user_id, interest_id) values (1,1), (1,2), (1,3)

P.S. I do understand correctly that the interests are a classifier, and adding new rows to the interests table is beyond the scope of this question, right?

3 Comments

Oracle has a way to insert into more than one table with a single INSERT: download.oracle.com/docs/cd/B19306_01/server.102/b14200/…
Ahh, well then, I take my words back. :P
That's correct, the interests table is a set number of choices the user can select.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.