views:

92

answers:

2

This problem is baffling me:

BEGIN;
INSERT INTO sub_users(user_id, email) 
SELECT user_id FROM users WHERE email='[email protected]', '$email';
COMMIT;

Normally, I have multiple statements in that transactions, but I've removed those for clarity.

I get this error:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near ' '[email protected]'' at line 1

I'm using MySQL version 5.1.36

+2  A: 

Two problems with your statement:

  1. You're not selecting email to insert (only id)
  2. You're using '=' instead of IN clause.

Should be something like:

INSERT INTO sub_users(user_id, email)
 SELECT user_id, email FROM users
  WHERE email IN ('[email protected]', '[email protected]');

instead.

Update (based on comment)

INSERT INTO sub_users(user_id, email)
 SELECT user_id, '[email protected]' FROM users
  WHERE email = '[email protected]';
ChssPly76
The email I want to insert is in the table I'm selecting from.
Chad
You're selecting from `users`. The above query will select all records from `users` whose `email` field is '[email protected]' or '[email protected]' and insert those records into `sub_users`. If you want to do something else, please clarify your question
ChssPly76
Ok, I want to take the user_id of the user with email [email protected], then insert that into the sub_users table along with a new email ([email protected]). So [email protected] will come from a php variable, not the database.
Chad
I see. You need to specify your new email as constant value (or a parameter you're going to bind) in select clause. I've updated my answer above with a sample select.
ChssPly76
[email protected] should not come from any mysql table. It will be defined by the user so it will be PHP variable.
Chad
It's **NOT** coming from any mysql table. How are you creating the insert / select statement above in PHP? By concatenating the strings? So just replace `[email protected]` with your PHP variable - insert it into query the same way you're inserting `[email protected]`
ChssPly76
sorry, i assumed that [email protected] was part of the select statement; that's what it looks like. I tested it out and it works. Thanks.
Chad
+2  A: 

You have at least two errors that I can see:

  1. You are trying to insert two columns worth of data (user_id and email) but only selecting one column (user_id) from the second half of INSERT INTO ... SELECT. You must select the same number of columns as you are trying to insert, and order matters.
  2. You have a syntax error in the predicate of your SELECT. I think you want to use:

    WHERE email IN ('[email protected]', '[email protected]')

or its equivalent:

WHERE email = '[email protected]'
   OR email = '[email protected]'
dcrosta
But what if the second value I want to insert is not to come from the select. What do I do then?
Chad
I only need the user_id from the users table.
Chad
@Chad, you can do `INSERT INTO sub_users (user_id, email) SELECT user_id, 'some constant' FROM ...`. `'some constant'` can also be a function or another field from the table you're selecting from.
dcrosta
no, the email will not come from any table, it will be from a PHP variable.
Chad
I've reposted here: http://stackoverflow.com/questions/1622916/simple-mysql-query-question
Chad
OK, so set the `'some constant'` from PHP. `INSERT INTO sub_users (user_id, email) SELECT user_id, '$myVarSqlEscaped' FROM ...`
dcrosta
thanks dcrosta.
Chad