Ryan Castle Ryan Castle - 5 months ago 8
SQL Question

SQL Queries - Insert Statement with Select (only using one column of select)

So, I'm writing a query to insert a record into a table which needs to select part of the inserting data from another database. The following is what I have so far:

INSERT INTO
teams_views (teamId, viewerId)
VALUES (
(SELECT teamId FROM teams WHERE teamName = 'name'),
1
);


I have searched about and have found loads of insert selection answers, but I haven't been able to find any queries for a single column selection. What can I do with my query to make this possible?

Thanks!

Answer

http://sqlfiddle.com/#!9/e2a75/2

INSERT INTO
    teams_views (teamId, viewerId)
SELECT teamId,1
FROM teams 
WHERE teamName = 'name';
Comments