Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Query to insert values from one table into another

I have a 2 table structure:

Table A:

RID; Name; LP
E    F     1
E    F     2
E    F     3
E    F     12
E    F     152

Table B:

LP
1
2
3
12
152
...
156
157
180
itd.

Query something like:

Insert into TableA(RID, Name, LP) Values ('E', 'F', Select LP from TableB)

I want to achive:

RID; Name; LP
E    F     1
E    F     2
E    F     3
E    F     12
E    F     152
E    F     156
E    F     157
E    F     180
etc.
like image 801
mike00 Avatar asked Dec 21 '22 04:12

mike00


2 Answers

You're close. Try

INSERT INTO TableA(RID, Name, LP)
SELECT 'E', 'F', LP FROM TableB
-- Omit this where clause, if duplicate LP's are OK
WHERE TableB.LP NOT IN (SELECT LP FROM TableA)
like image 138
Lukas Eder Avatar answered Dec 25 '22 22:12

Lukas Eder


Insert into TableA(RID, Name, LP) 
Select 
   'E', 'F', LP 
 from 
   TableB
like image 29
Florin stands with Ukraine Avatar answered Dec 25 '22 23:12

Florin stands with Ukraine