Solved

How do you do a sub query with different tables?

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

932 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

14 Experts available now in Live!

Get 1:1 Help Now