Solved

Sql query combinational join

Posted on 2010-11-29
5
489 Views
Last Modified: 2012-08-13
Say i have a table A and table B

table A has 5 columns - 1,2 ,3,4,5
table B has 2 columns -1,2

I need to select the data from table A(3,4,5) for which the combination of data in table A and B must be same(table A 1,2 and table B 1,2)

table A
1 2 3 4 5
a b c d e
a c c d e
b c e f g

tableB
1 2
a c
b c

output:
table A
3 4 5
c d e
e f g

Sybase database.

0
Comment
Question by:magento
  • 2
  • 2
5 Comments
 
LVL 14

Expert Comment

by:leoahmad
ID: 34231411
select col1,col2,col3 from table1 where col1 in (select col1 from table2) and col2 in (select col2 from table2)
0
 
LVL 14

Assisted Solution

by:leoahmad
leoahmad earned 200 total points
ID: 34231420
better

select col3,col4,col5 from table1 where col1 in (select col1 from table2) and col2 in (select col2 from table2)
0
 
LVL 14

Accepted Solution

by:
Jan_Franek earned 300 total points
ID: 34231437
select col3, col4, col5
from tableA a, tableB b
where a.col1 = b.col1
and a.col2 = b.col2
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34232537
Jan's query is correct
However, I would use the Ansi style join, and also add the alias prefix to all columns as good standard practice

select a.col3, a.col4, a.col5
from tableA a
inner join tableB b on a.col1 = b.col1 and a.col2 = b.col2
0
 
LVL 14

Expert Comment

by:Jan_Franek
ID: 34236871
Author did not specify, what version of Sybase database is he using. AFAIK some old versions did not support ANSI joins, so I decided to use non-ANSI supported on all Sybase databases.
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

685 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