Solved

Sql query combinational join

Posted on 2010-11-29
5
468 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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 …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 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

15 Experts available now in Live!

Get 1:1 Help Now