Solved

How do you do a sub query with different tables?

Posted on 2009-07-16
3
274 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
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 59

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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
Via a live example, show how to shrink a transaction log file down to a reasonable size.

757 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