1

I can't get the following Insert to work although the syntax seems correct..

INSERT INTO views T
    JOIN members T2
        ON '$username' = T2.username 
(ITEM_ID, ITEM_TYPE, USER_ID, USER_TYPE) 
VALUES('$itemview', '$type', T2.id, '$usertype')

All the variables are predefined of course..
What am I doing wrong here?

The Error: 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 'T JOIN members T2 ON 'testuser' = T2.username (ITEM_ID, IT' at line 1

2
  • How can you join two tables if the record hasn't been created yet? Commented Jul 11, 2013 at 23:45
  • This question's answer may be able to help you out. Commented Jul 11, 2013 at 23:46

1 Answer 1

2

If I'm understanding you right, you want this syntax instead:

INSERT INTO views
  (ITEM_ID, ITEM_TYPE, USER_ID, USER_TYPE)
SELECT '$itemview', '$type', id, '$usertype'
FROM members
WHERE username = '$username'

It inserts a record into views with the partial contents of members. The number of rows returned with the SELECT also determines the number of inserted records; you may wish to use LIMIT if that's actually a problem.

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

Comments

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.