Solved

SQL JOIN when no columns are in common

Posted on 2011-03-06
8
456 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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 41

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This video shows how to recover a database from a user managed backup
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

679 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