Solved

Select parent data where child data is same as search info

Posted on 2008-10-25
10
230 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
 

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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

867 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

22 Experts available now in Live!

Get 1:1 Help Now