techhunter techhunter - 5 months ago 24
SQL Question

insert values from different tables

I am working in ORACLE DB for one of my projects. Now i have to insert the values to one table from 2 different tables.

Table created a below:

CREATE TABLE ATRB_VALUES_COUNT(MID VARCHAR(20),GID VARCHAR(20),SID VARCHAR(20),AID VARCHAR(20),AVID VARCHAR(20))


The values to be inserted are as below:


  1. Get AID,AVID FROM Table called ATRB_VAL

  2. Get Corresponding SID from AID from table ATRB

  3. Get Corresponding MID,GID from table SB_ATRB



I have tried using union. But it din't work as there are different number of rows.

Answer Source

you should use join not union

insert into ATRB_VALUES_COUNT(MID,GID,SID,AID,AVID)
select a.AID,a.AVID, b.SID,c.MID,c.GID from ATRB_VAL as a, ATRB as b, SB_ATRB as c  

(your join here, join 3 tables` ATRB_VAL , ATRB, SB_ATRB )