Solved

SQL Select help

Posted on 2009-05-12
14
226 Views
Last Modified: 2012-05-06
I have 2 tables.

table 1
id     name
1      group 1
2      group 2

table 2
id        id of first table
1         1
2         1
3         1

What I would like to do is to SELECT from the first table only if it has records relating to it in the second table. So in the example above, only the record with ID 1 would be seleted.

Any ideas?

Thanks
0
Comment
Question by:thomasmutton
  • 7
  • 6
14 Comments
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24364449
select t1.id,t1.name from table1 t1 join table2 t2 on t1.id=t2.IdOfFirstTable
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24364455
You can use the INNER join

SELECT t1.*
FROM Table1 t1
INNER join table2 t2 on t1.ID =t2.iD
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24364476
option 2)


select t1.id,t1.name
from
table1 t1
left outer join
table2 t2 on
t1.id=t2.IdOfFirstTable
where t2.id is not null
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24364483
aneeshattingal,

I guess join should be on t1.ID =t2.IdOfFirstTable not with t2.id........
0
 

Author Comment

by:thomasmutton
ID: 24364501
RiteshShah,

I have tried your second option but it does not deliver distinct records. Should I use a distinct?
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24364520
you can use it, but what happened to my first query?
0
 

Author Comment

by:thomasmutton
ID: 24364528
They have the same outcome
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 31

Expert Comment

by:RiteshShah
ID: 24364560
in that case, I recomend you to go for option 1, it is proper method to go.
0
 

Author Comment

by:thomasmutton
ID: 24364577
Ok well this is the command I am using.

it does not work.

SELECT * FROM Gower_tbl_photosforsaleoptiongroups pfsog
JOIN Gower_tbl_photosforsalegroupoptions pfsgo ON
pfsog.pfsog_Id = pfsgo.pfsog_Id
0
 
LVL 31

Accepted Solution

by:
RiteshShah earned 500 total points
ID: 24364591
is this giving you any error?

SELECT pfsog.* FROM Gower_tbl_photosforsaleoptiongroups pfsog
JOIN Gower_tbl_photosforsalegroupoptions pfsgo ON
pfsog.pfsog_Id = pfsgo.pfsog_Id
0
 

Author Comment

by:thomasmutton
ID: 24364602
Nope, It is getting the right record from table 1 but just repeating it 3 times.
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24364615
in that case, use DISTINCT....
0
 

Author Comment

by:thomasmutton
ID: 24364645
Yeah that worked great.

SELECT DISTINCT pfsog.* FROM Gower_tbl_photosforsaleoptiongroups pfsog
JOIN Gower_tbl_photosforsalegroupoptions pfsgo ON
pfsog.pfsog_Id = pfsgo.pfsog_Id
0
 

Author Closing Comment

by:thomasmutton
ID: 31580524
SELECT DISTINCT pfsog.* FROM Gower_tbl_photosforsaleoptiongroups pfsog
JOIN Gower_tbl_photosforsalegroupoptions pfsgo ON
pfsog.pfsog_Id = pfsgo.pfsog_Id
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

920 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

13 Experts available now in Live!

Get 1:1 Help Now