Solved

SQL JOIN when no columns are in common

Posted on 2011-03-06
8
450 Views
Last Modified: 2012-05-11
Just wondering how to join 2 tables when no columns are in common.

Like

Table A => name, place, item
TABLE B=> type, rule , value

Now I want to join table A and B. Item in Table A has same data and datatype same as type in table B. But their column names are not same.
0
Comment
Question by:pal2k
8 Comments
 
LVL 14

Expert Comment

by:ajexpert
ID: 35047227
TRY UNION ALL

SELECT NAME, PLACE, ITEM
FROM TABLEA
UNION ALL
SELECT TYPE_RULE_VALUE
FROM TABLEB
0
 

Author Comment

by:pal2k
ID: 35047293
thks...

Wondering if UNION will be still good if I need to add a WHERE clause in TABLE A by column Item
0
 

Author Comment

by:pal2k
ID: 35047483
also the no of columns are not same in both tables...
0
 

Accepted Solution

by:
NewDan526 earned 125 total points
ID: 35048735
Column names don't have to be the same when joining.  The USING clause or a "NATURAL JOIN" use the column names, but you should use the "ON" clause here.


SELECT a.*, b.*
  FROM a JOIN b
    ON (a.item = b.type)
 WHERE item = "xxx"
;
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 14

Expert Comment

by:ajexpert
ID: 35048772
There should not be any issues if you include WHERE clause in TABLEA.

You can still work with UNION ALL even if number of columns in both tables are not same, provided you make sure that you list the column names AND it should have same datatypes in TABLEA and TABLEB
0
 
LVL 14

Assisted Solution

by:ajexpert
ajexpert earned 125 total points
ID: 35049057
Let me ask you the following before we comment.

1.  Is the data in TABLEA and TABLEB is same even though the column names are different?  If yes, which columns in TABLEA and TABLEB have the same data?

2.  How you exepect the output? listing all columns from TABLEA and TABLEB or just combining the records from TABLEA and TABLEB?
0
 
LVL 40

Assisted Solution

by:Sharath
Sharath earned 125 total points
ID: 35050314
Try CROSS JOIN.
SELECT *
  FROM Table_A, Table_B

Open in new window

0
 
LVL 5

Assisted Solution

by:jaiminpsoni
jaiminpsoni earned 125 total points
ID: 35060312
Select * from table_A a , table_B b where a.item = b.type;

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now