0

I am trying to create an insert statement, below is what I have so far:

INSERT INTO Role (application, developer, role) 

VALUES (? , 
(SELECT Id from Developer WHERE firstName='Alice' and lastname='Wonderland'), 
'ARCHITECT');

In the missing value field, I am trying to do a join, the following join statement returns the VALUES I need:

   SELECT a.id 
FROM Application a
JOIN application d ON d.id = a.id and a.category='GAMES'

However, when I insert the previous query into the missing value field in the insert statement, it gives me an error stating that the query returned more than 1 row (which is intentional, seeing as I want ALL rows returned to be inserted into the role table)

Can anyone provide me feedback on how to fix this issue?

5
  • You can only insert one row at a time, so you'll need to find a way to do this one-by-one. Commented Oct 20, 2014 at 15:09
  • That join you have makes no sense. You are joining the same table on the same id column. Are you sure this is correct? Commented Oct 20, 2014 at 15:10
  • @Donal, yes the join statement works fine outside of the value field. If you have a better way to setup the join statement, let me know. Commented Oct 20, 2014 at 15:12
  • @McAdam331: you can insert more than one row at a time. Either by using insert into ... values (1,1), (2,2) or by using insert into ... select ... Commented Oct 20, 2014 at 15:13
  • @a_horse_with_no_name, good catch. My mistake. Also, OP, I think Donal's question is, there's no need to join a table with itself. Why not just select from that table? A simpler way to get all GAMES applications would be: SELECT id FROM application WHERE category = 'GAMES'; Commented Oct 20, 2014 at 15:16

3 Answers 3

1

I think this is what you are looking for:

INSERT INTO Role(application, developer, role)
    SELECT a.id, (SELECT Id from Developer WHERE firstName = 'Alice' and lastname = 'Wonderland'),
           'ARCHITECT' 
    FROM Application a JOIN
         application d
         ON d.id = a.id and a.category = 'GAMES';

Note: I was going to move the subquery with Developer into the FROM clause. However, if there are no matches, this will return NULL -- which seems to be your intention. If I put it in the FROM clause, you will get no matches (MySQL doesn't have a left cross join).

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

Comments

0

Incorporate your other parameters into your select statement

INSERT INTO Role (application, developer, role)
SELECT ?, Id, 'ARCHITECT' FROM Application a 
JOIN application d ON d.id = a.id and a.category='GAMES'

2 Comments

How would this look? (the SQL statement) I don't get how to create an insert statement that handles every element I need, as well as the join without using a value field.
Just like this. An INSERT INTO with a SELECT statement after it will insert all the rows returned by the SELECT
0

You need to use INSERT INTO .. SELECT FROM construct to get this done. A mock up query would look like below

INSERT INTO Role (application, developer, role)    
select a.id,
dev.Id,
'ARCHITECT'
FROM Application a
JOIN developer dev on a.Id = dev.Id
WHERE a.category='GAMES'
AND dev.firstName='Alice' 
and dev.lastname='Wonderland'

2 Comments

In this case how would i set the value of "role"? Its a string. Also, the developer's first name and last name are used to obtain its developer id.
Follow along the answer. Hope that helps.

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.