Solved

SQL Select help

Posted on 2009-05-12
14
231 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Webinar: Choosing a MySQL HA Solution

Join Percona’s Principal Technical Services Engineer, Marcos Albe as he presents Choosing a MySQL High Availability Solution on Thursday, June 29, 2017 at 10:00 am PDT / 2:00 pm EDT (UTC-7).

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

729 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