Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8420591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:55:01+00:00 2026-06-10T02:55:01+00:00

Hey all i am trying to figure out how to go about inserting a

  • 0

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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:55:01+00:00Added an answer on June 10, 2026 at 2:55 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey all i am trying to figure out how to go about calculating this
Hey all I'm trying to do the following insert query SqlDataSource userQuizDataSource = new
Hey all i am in need of some help trying to figure out how
Hey all, i am trying to figure out how to calculate the wage for
Hey all, i am in need of some help trying to figure out why
Hey all. I've been been trying to figure this out for a while now.
Hey All you out there, I am trying to write a query that sums
Hey all i am trying to figure out a way to see what all
Hey I'm trying to figure something out.. I want to get all Carts that
Hey all i am trying to search through a CSV file using the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.