Solved

How do you do a sub query with different tables?

Posted on 2009-07-16
3
290 Views
Last Modified: 2012-05-07
I'm trying to combine 2 queries into 1.  I'm just learning sub queries but haven't hit on the right solution to make this query give me the results I need.

Basically, I have 2 tables (modified smaller schema below) where I have folder id and names in one table and folder ids and content ids in the other.  I need to be able to run a query where I can get the folder name and content id from 2 different tables using the folder ID.  This is to be used for building dynamic web links in a menu.

Thanks in advance for the help!
Schema: menu_tbl
================
mnu_id (int) - PK
mnu_name (nvarchar(255))
 
Schema: menu_to_item_tbl
====================
mnu_id (int)
item_id (int)
item_title (nvarchar(255))

Open in new window

0
Comment
Question by:saabStory
[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
3 Comments
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 250 total points
ID: 24871522
A simple join would suffice:


select menu_tbl.mnu_name, menu_to_item_tbl.item_title
from menu_tbl inner join menu_to_item_tbl
on menu_tbl.mnu_id = menu_to_item_tbl.mnu_id

Open in new window

0
 
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 250 total points
ID: 24871528
Hello saabStory

You will want to use a JOIN statement.  If you only want records where you have a match (data exists for mnu_id in both tables, then use an INNER JOIN; otherwise, use an OUTER JOIN.

Regards,

mwvisa1
-- e.g., an inner join would look like this; just change 'inner' to 'left outer' if not always a matching record.
select f.mnu_name, c.item_id, c.item_title
from menu_tbl f
inner join menu_to_item_tbl c on c.mnu_id = f.mnu_id;

Open in new window

0
 

Author Closing Comment

by:saabStory
ID: 31604326
Thanks to you both.  I just learned about sub-queries and had blocked out the more obvious approach.  Thanks for the help!
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.
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.

695 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