Solved

SQL Select help

Posted on 2009-05-12
14
225 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

707 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

12 Experts available now in Live!

Get 1:1 Help Now