Solved

Select parent data where child data is same as search info

Posted on 2008-10-25
10
236 Views
Last Modified: 2012-05-05
I have two tables.

Table 1: ForumList, (parent)
ForumListId, parent
ForumListName

Table 2: Forum, (child)
ForumId
ForumListId
ForumText

Now I want to select all ForumList where the child Forum have ForumText = search string (use LIKE).
The SQL result have to be the table ForumList, like SELECT * FROM ForumList ....
How to do that?
0
Comment
Question by:KimGordon
  • 4
  • 3
  • 3
10 Comments
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 100 total points
ID: 22802764
SELECT fl.ForumListID, fl.ForumListName, f.ForumID, f.ForumText
FROM ForumList fl INNER JOIN
    Forum f ON fl.ForumListID = fl.ForumListID
WHERE f.ForumText LIKE '%foo%'
0
 
LVL 2

Accepted Solution

by:
Clausewitz earned 150 total points
ID: 22802776
Just use a subquery.
SELECT * FROM ForumList WHERE ForumListId IN
(
  SELECT DISTINCT ForumListId FROM Forum WHERE ForumText LIKE -- your search term
)

Open in new window

0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 22802914
Clausewitz,

That will work, but won't that be less efficient than a join?

Regards,

Patrick
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:KimGordon
ID: 22803121
I'm using the subquery alternative now and it works!
Thanks!

0
 

Author Comment

by:KimGordon
ID: 22803134
Is this solution less efficient?
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 22803159
Have you tried my suggestion?
0
 

Author Comment

by:KimGordon
ID: 22803304
No, should I? Is this one better?
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 22803355
KimGordon said:
>>No, should I?

Yes, you should.

>>Is this one better?

That is for you to decide.  On large data sets, my query will tend to run faster.
0
 
LVL 2

Expert Comment

by:Clausewitz
ID: 22803402
i think the subquery will be faster, because a join should be more expensive.
Though the KimGordon doesn't need any data fields from the child table a join has no benefits over a subquery.
0
 
LVL 2

Expert Comment

by:Clausewitz
ID: 22803469
Best you run both queries and look at the execution plan and the execution time to compare both queries to be sure which one runs faster.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

770 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