Solved

How do you do a sub query with different tables?

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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

738 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