Solved

Sql query combinational join

Posted on 2010-11-29
5
484 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQl Query to find x consecutive Nbrs in a Table 30 97
Addition to SQL for dynamic fields 6 47
Run SQL Server Proc from Access 11 37
performance query 4 32
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

840 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