Solved

How do you do a sub query with different tables?

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Query Task 11 43
Help needed in sql query 4 28
SQL Instance service gone? 5 36
Need to trim my database size 9 19
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

756 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