Solved

SQL Select help

Posted on 2009-05-12
14
230 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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
 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying 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
T-SQL to copy a database 37 66
Connect remotely to SQL Server DB without TCP/IP enabled? 2 36
Related to SQL Query 5 21
Help with Progress 4gl Rounding Function 6 27
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

733 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