MySQL Forums
Forum List  »  Newbie

Insert data from CONCAT data from one table using select statement into new table
Posted by: Tim burke
Date: May 26, 2019 08:15AM

This it what I have so far but I think I'm doing something wrong.

INSERT INTO username (username)
SELECT (concat(substring(LastName,1,2),substring(IDNumber,1,4))) FROM person
WHERE person.IDNumber = username.IDNumber;

username is the new table and the column name is also username. I'm trying to automatically create usernames from the first 2 digits of the users last name and the first 4 digits of their ID Number. The users last name and ID number are located in a table called "person".

This is the statement I used to create the new table called username.

CREATE TABLE username (
usernameID INT AUTO_INCREMENT PRIMARY KEY,
IDNumber INT NOT NULL,
username CHAR(6) NOT NULL,
FOREIGN KEY (IDNumber) REFERENCES person(IDNumber));

This table is empty at the moment but I want it to populate using the data from the persons table automatically. Hopefully someone can direct me how to do this.

Thanks

Options: ReplyQuote


Subject
Written By
Posted
Insert data from CONCAT data from one table using select statement into new table
May 26, 2019 08:15AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.