1

I've the following query:

INSERT INTO 
                                table
                            (
                                memberid,
                                field1,
                                field2,
                                field3
                            )  
                            VALUES
                            (
                                :memberid,              
                                (
                                    SELECT
                                        field1,
                                        field2,
                                        field3
                                    FROM 
                                        table
                                    WHERE 
                                        id = :id
                                )
                            )

I'm using PDO prepared statements, but the query doesn't work in this way.

Does anyone know how to combine prepared statements and SELECT variables into one query?

Tnx

3
  • What SQL implementation do you use? Commented Aug 11, 2010 at 16:09
  • I'm sorry Victor. I didn't know this feature is available. I will remember that in the future! Commented Aug 11, 2010 at 16:23
  • Please don’t answer on your own question if you don’t have an answer to your question. Commented Aug 11, 2010 at 16:32

1 Answer 1

3

Have you considered insert-select?

INSERT INTO mytable (a,b) 
SELECT :a, b
FROM myothertable
Sign up to request clarification or add additional context in comments.

3 Comments

I've tried that, but that isn't working. I get this error: Insert value list does not match column list: 1136 Column count doesn't match value count at row 1 in
You're probably using code like "INSERT INTO (a,b,c) SELECT a,b". Post the actual query and we'll count the columns and values for you.
Thank you. I made a mistake with setting the VALUES parameter (thanks Gumbo)

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.