Insert using LEFT JOIN and INNER JOIN










34















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 BBarker@priceisright.com 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','Bbarker@priceisright.com',0);
INSERT INTO user_permission (user_id, permission_id) VALUES (LAST_INSERT_ID(),4);









share|improve this question
























  • Why are you inserting into user when you have just selected values from user? What new row are you intending to insert?

    – Michael Berkowski
    Aug 22 '12 at 12:53











  • @AaronJSpetner when you propose an edit, please make it a complete edit so that subsequent edits are not required to fix casing, punctuation, and removal of useless text like "hey all".

    – mickmackusa
    Jul 8 '18 at 10:39















34















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 BBarker@priceisright.com 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','Bbarker@priceisright.com',0);
INSERT INTO user_permission (user_id, permission_id) VALUES (LAST_INSERT_ID(),4);









share|improve this question
























  • Why are you inserting into user when you have just selected values from user? What new row are you intending to insert?

    – Michael Berkowski
    Aug 22 '12 at 12:53











  • @AaronJSpetner when you propose an edit, please make it a complete edit so that subsequent edits are not required to fix casing, punctuation, and removal of useless text like "hey all".

    – mickmackusa
    Jul 8 '18 at 10:39













34












34








34


12






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 BBarker@priceisright.com 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','Bbarker@priceisright.com',0);
INSERT INTO user_permission (user_id, permission_id) VALUES (LAST_INSERT_ID(),4);









share|improve this question
















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 BBarker@priceisright.com 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','Bbarker@priceisright.com',0);
INSERT INTO user_permission (user_id, permission_id) VALUES (LAST_INSERT_ID(),4);






mysql left-join inner-join






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 8 '18 at 13:55









Aaron J Spetner

1,51711324




1,51711324










asked Aug 22 '12 at 12:47









StealthRTStealthRT

4,49927136255




4,49927136255












  • Why are you inserting into user when you have just selected values from user? What new row are you intending to insert?

    – Michael Berkowski
    Aug 22 '12 at 12:53











  • @AaronJSpetner when you propose an edit, please make it a complete edit so that subsequent edits are not required to fix casing, punctuation, and removal of useless text like "hey all".

    – mickmackusa
    Jul 8 '18 at 10:39

















  • Why are you inserting into user when you have just selected values from user? What new row are you intending to insert?

    – Michael Berkowski
    Aug 22 '12 at 12:53











  • @AaronJSpetner when you propose an edit, please make it a complete edit so that subsequent edits are not required to fix casing, punctuation, and removal of useless text like "hey all".

    – mickmackusa
    Jul 8 '18 at 10:39
















Why are you inserting into user when you have just selected values from user? What new row are you intending to insert?

– Michael Berkowski
Aug 22 '12 at 12:53





Why are you inserting into user when you have just selected values from user? What new row are you intending to insert?

– Michael Berkowski
Aug 22 '12 at 12:53













@AaronJSpetner when you propose an edit, please make it a complete edit so that subsequent edits are not required to fix casing, punctuation, and removal of useless text like "hey all".

– mickmackusa
Jul 8 '18 at 10:39





@AaronJSpetner when you propose an edit, please make it a complete edit so that subsequent edits are not required to fix casing, punctuation, and removal of useless text like "hey all".

– mickmackusa
Jul 8 '18 at 10:39












2 Answers
2






active

oldest

votes


















42














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





share|improve this answer

























  • I am in need of inserting a row in the user_permission table as well along side the user table. How would i do that?

    – StealthRT
    Aug 22 '12 at 12:58












  • Updated my OP to better explain what i was looking to do.

    – StealthRT
    Aug 22 '12 at 13:06






  • 1





    @StealthRT You can't insert new rows into 2 tables without multiple statements, or a trigger on the first table.

    – Michael Berkowski
    Aug 22 '12 at 13:07











  • I updated my OP again to reflect what i am thinking you mean by your post. Let me know if its correct. Would i need to run "update" between the 2 inserts?

    – StealthRT
    Aug 22 '12 at 13:11






  • 2





    @StealthRT Yes, just like that - but you must execute the second immediately after the first, or the value of LAST_INSERT_ID() will become invalid. The LAST_INSERT_ID() is connection dependent, so as long as these are executed sequentially on the same connection, you'll get the right value back. If any other inserts happen in between, you'll get the wrong value.

    – Michael Berkowski
    Aug 22 '12 at 13:18


















18














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
);





share|improve this answer























  • Thanks for taking the time to reply with an answer, Omesh!

    – StealthRT
    Aug 22 '12 at 13:36










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12073513%2finsert-using-left-join-and-inner-join%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









42














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





share|improve this answer

























  • I am in need of inserting a row in the user_permission table as well along side the user table. How would i do that?

    – StealthRT
    Aug 22 '12 at 12:58












  • Updated my OP to better explain what i was looking to do.

    – StealthRT
    Aug 22 '12 at 13:06






  • 1





    @StealthRT You can't insert new rows into 2 tables without multiple statements, or a trigger on the first table.

    – Michael Berkowski
    Aug 22 '12 at 13:07











  • I updated my OP again to reflect what i am thinking you mean by your post. Let me know if its correct. Would i need to run "update" between the 2 inserts?

    – StealthRT
    Aug 22 '12 at 13:11






  • 2





    @StealthRT Yes, just like that - but you must execute the second immediately after the first, or the value of LAST_INSERT_ID() will become invalid. The LAST_INSERT_ID() is connection dependent, so as long as these are executed sequentially on the same connection, you'll get the right value back. If any other inserts happen in between, you'll get the wrong value.

    – Michael Berkowski
    Aug 22 '12 at 13:18















42














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





share|improve this answer

























  • I am in need of inserting a row in the user_permission table as well along side the user table. How would i do that?

    – StealthRT
    Aug 22 '12 at 12:58












  • Updated my OP to better explain what i was looking to do.

    – StealthRT
    Aug 22 '12 at 13:06






  • 1





    @StealthRT You can't insert new rows into 2 tables without multiple statements, or a trigger on the first table.

    – Michael Berkowski
    Aug 22 '12 at 13:07











  • I updated my OP again to reflect what i am thinking you mean by your post. Let me know if its correct. Would i need to run "update" between the 2 inserts?

    – StealthRT
    Aug 22 '12 at 13:11






  • 2





    @StealthRT Yes, just like that - but you must execute the second immediately after the first, or the value of LAST_INSERT_ID() will become invalid. The LAST_INSERT_ID() is connection dependent, so as long as these are executed sequentially on the same connection, you'll get the right value back. If any other inserts happen in between, you'll get the wrong value.

    – Michael Berkowski
    Aug 22 '12 at 13:18













42












42








42







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





share|improve this answer















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






share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 22 '14 at 18:34

























answered Aug 22 '12 at 12:50









Michael BerkowskiMichael Berkowski

224k34375346




224k34375346












  • I am in need of inserting a row in the user_permission table as well along side the user table. How would i do that?

    – StealthRT
    Aug 22 '12 at 12:58












  • Updated my OP to better explain what i was looking to do.

    – StealthRT
    Aug 22 '12 at 13:06






  • 1





    @StealthRT You can't insert new rows into 2 tables without multiple statements, or a trigger on the first table.

    – Michael Berkowski
    Aug 22 '12 at 13:07











  • I updated my OP again to reflect what i am thinking you mean by your post. Let me know if its correct. Would i need to run "update" between the 2 inserts?

    – StealthRT
    Aug 22 '12 at 13:11






  • 2





    @StealthRT Yes, just like that - but you must execute the second immediately after the first, or the value of LAST_INSERT_ID() will become invalid. The LAST_INSERT_ID() is connection dependent, so as long as these are executed sequentially on the same connection, you'll get the right value back. If any other inserts happen in between, you'll get the wrong value.

    – Michael Berkowski
    Aug 22 '12 at 13:18

















  • I am in need of inserting a row in the user_permission table as well along side the user table. How would i do that?

    – StealthRT
    Aug 22 '12 at 12:58












  • Updated my OP to better explain what i was looking to do.

    – StealthRT
    Aug 22 '12 at 13:06






  • 1





    @StealthRT You can't insert new rows into 2 tables without multiple statements, or a trigger on the first table.

    – Michael Berkowski
    Aug 22 '12 at 13:07











  • I updated my OP again to reflect what i am thinking you mean by your post. Let me know if its correct. Would i need to run "update" between the 2 inserts?

    – StealthRT
    Aug 22 '12 at 13:11






  • 2





    @StealthRT Yes, just like that - but you must execute the second immediately after the first, or the value of LAST_INSERT_ID() will become invalid. The LAST_INSERT_ID() is connection dependent, so as long as these are executed sequentially on the same connection, you'll get the right value back. If any other inserts happen in between, you'll get the wrong value.

    – Michael Berkowski
    Aug 22 '12 at 13:18
















I am in need of inserting a row in the user_permission table as well along side the user table. How would i do that?

– StealthRT
Aug 22 '12 at 12:58






I am in need of inserting a row in the user_permission table as well along side the user table. How would i do that?

– StealthRT
Aug 22 '12 at 12:58














Updated my OP to better explain what i was looking to do.

– StealthRT
Aug 22 '12 at 13:06





Updated my OP to better explain what i was looking to do.

– StealthRT
Aug 22 '12 at 13:06




1




1





@StealthRT You can't insert new rows into 2 tables without multiple statements, or a trigger on the first table.

– Michael Berkowski
Aug 22 '12 at 13:07





@StealthRT You can't insert new rows into 2 tables without multiple statements, or a trigger on the first table.

– Michael Berkowski
Aug 22 '12 at 13:07













I updated my OP again to reflect what i am thinking you mean by your post. Let me know if its correct. Would i need to run "update" between the 2 inserts?

– StealthRT
Aug 22 '12 at 13:11





I updated my OP again to reflect what i am thinking you mean by your post. Let me know if its correct. Would i need to run "update" between the 2 inserts?

– StealthRT
Aug 22 '12 at 13:11




2




2





@StealthRT Yes, just like that - but you must execute the second immediately after the first, or the value of LAST_INSERT_ID() will become invalid. The LAST_INSERT_ID() is connection dependent, so as long as these are executed sequentially on the same connection, you'll get the right value back. If any other inserts happen in between, you'll get the wrong value.

– Michael Berkowski
Aug 22 '12 at 13:18





@StealthRT Yes, just like that - but you must execute the second immediately after the first, or the value of LAST_INSERT_ID() will become invalid. The LAST_INSERT_ID() is connection dependent, so as long as these are executed sequentially on the same connection, you'll get the right value back. If any other inserts happen in between, you'll get the wrong value.

– Michael Berkowski
Aug 22 '12 at 13:18













18














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
);





share|improve this answer























  • Thanks for taking the time to reply with an answer, Omesh!

    – StealthRT
    Aug 22 '12 at 13:36















18














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
);





share|improve this answer























  • Thanks for taking the time to reply with an answer, Omesh!

    – StealthRT
    Aug 22 '12 at 13:36













18












18








18







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
);





share|improve this answer













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
);






share|improve this answer












share|improve this answer



share|improve this answer










answered Aug 22 '12 at 12:50









OmeshOmesh

19.5k63146




19.5k63146












  • Thanks for taking the time to reply with an answer, Omesh!

    – StealthRT
    Aug 22 '12 at 13:36

















  • Thanks for taking the time to reply with an answer, Omesh!

    – StealthRT
    Aug 22 '12 at 13:36
















Thanks for taking the time to reply with an answer, Omesh!

– StealthRT
Aug 22 '12 at 13:36





Thanks for taking the time to reply with an answer, Omesh!

– StealthRT
Aug 22 '12 at 13:36

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12073513%2finsert-using-left-join-and-inner-join%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown