Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert using LEFT JOIN and INNER JOIN

Hey all i am trying to figure out how to go about inserting a new record using the following query:

SELECT user.id, user.name, user.username, user.email,    IF(user.opted_in = 0, 'NO', 'YES') AS optedIn   FROM    user   LEFT JOIN user_permission AS userPerm ON user.id = userPerm.user_id ORDER BY user.id; 

My INSERT query so far is this:

INSERT INTO user  SELECT *  FROM user    LEFT JOIN user_permission AS userPerm ON user.id = userPerm.user_id; 

However, i am not sure how to do VALUE('','','','', etc etc) when using left and inner joins.

So what i am looking to do is this:

User table:

id    | name       | username    | password                 | OptIn -------------------------------------------------------------------- 562     Bob Barker   bBarker       [email protected]   1 

And also the user_permission table

user_id   | Permission_id ------------------------- 562         4 

UPDATE So like this?

INSERT INTO user (name, username, password, email, opted_in) VALUES ('Bbarker','Bbarker','blahblahblah','[email protected]',0); INSERT INTO user_permission (user_id, permission_id) VALUES (LAST_INSERT_ID(),4); 
like image 361
StealthRT Avatar asked Aug 22 '12 12:08

StealthRT


People also ask

Can we use insert with join?

Example 5: INSERT INTO SELECT statement with Join clause to get data from multiple tables. We can use a JOIN clause to get data from multiple tables. These tables are joined with conditions specified with the ON clause. Suppose we want to get data from multiple tables and insert into a table.

Can we use inner join in insert query?

SQL server insert multiple rows using an inner join would be possible. -- Insert into line will select the columns.

Can we use left outer join and inner join together?

The LEFT OUTER join will retrieve all rows from the table on the left of the join whether they match or not. In the subsequent INNER JOIN, obviously if something doesn't match up, you will lose some rows.

When to use inner join and left join?

You'll use INNER JOIN when you want to return only records having pair on both sides, and you'll use LEFT JOIN when you need all records from the “left” table, no matter if they have pair in the “right” table or not.


2 Answers

You have to be specific about the columns you are selecting. If your user table had four columns id, name, username, opted_in you must select exactly those four columns from the query. The syntax looks like:

INSERT INTO user (id, name, username, opted_in)   SELECT id, name, username, opted_in    FROM user LEFT JOIN user_permission AS userPerm ON user.id = userPerm.user_id 

However, there does not appear to be any reason to join against user_permission here, since none of the columns from that table would be inserted into user. In fact, this INSERT seems bound to fail with primary key uniqueness violations.

MySQL does not support inserts into multiple tables at the same time. You either need to perform two INSERT statements in your code, using the last insert id from the first query, or create an AFTER INSERT trigger on the primary table.

INSERT INTO user (name, username, email, opted_in) VALUES ('a','b','c',0); /* Gets the id of the new row and inserts into the other table */ INSERT INTO user_permission (user_id, permission_id) VALUES (LAST_INSERT_ID(), 4) 

Or using a trigger:

CREATE TRIGGER creat_perms AFTER INSERT ON `user` FOR EACH ROW BEGIN   INSERT INTO user_permission (user_id, permission_id) VALUES (NEW.id, 4) END 
like image 59
Michael Berkowski Avatar answered Sep 28 '22 03:09

Michael Berkowski


you can't use VALUES clause when inserting data using another SELECT query. see INSERT SYNTAX

INSERT INTO user (  id, name, username, email, opted_in ) (     SELECT id, name, username, email, opted_in     FROM user          LEFT JOIN user_permission AS userPerm             ON user.id = userPerm.user_id ); 
like image 20
Omesh Avatar answered Sep 28 '22 03:09

Omesh