1

I have the following insert query with a subquery:

INSERT INTO Order (time_of_purchase, cust_id) 
VALUES (NOW(), (SELECT cust_id FROM Customer WHERE first_name = "John" AND last_name = "Doe")) RETURNING reference_number

When I execute the query, postgres returns the error: "ERROR: column "John" does not exist. SQL state: 42703. Character: 110

What could be the problem?

1
  • Unlike MySQL default settings, proper SQL syntax dictates "identifier" and 'string'. Commented Mar 28, 2015 at 2:58

1 Answer 1

3

Use Single Quotes instead of double quotes. " makes the compiler to consider it as identifier. Try this insert

INSERT INTO Order (time_of_purchase, cust_id) 
SELECT NOW(),cust_id FROM Customer WHERE first_name = 'John' AND last_name = 'Doe'
Sign up to request clarification or add additional context in comments.

3 Comments

it works. but what if the first_name and last_name are parameters? Example: INSERT INTO Order (time_of_purchase, cust_id) SELECT NOW(),cust_id FROM Customer WHERE first_name = $var1 AND last_name = $var2
@jpineds Use parameter bindings (aka "prepared statements") then. Refer to the documentation of the Pg database adapter you are using (in PHP).
can it be achieved without using prepared statements?

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.