Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL Query

Posted on 2009-12-18
6
Medium Priority
?
220 Views
Last Modified: 2012-05-08
I have a table, tblsuggestedproducts, that has productid, suggestedproductid.  Both of these fields relate to the primary key, productid, in tblproducts

tblsuggestedproducts looks like this
productid      suggestedproductid
1                   3
1                   4
1                   7
2                   2
2                    3

I want to return a query that gives me the related data in tblproducts for both the tblsuggestedproducts.productid and the tblsuggestedproducts.suggestedproductid....any help would be appreciated.
0
Comment
Question by:cartch2008
  • 3
  • 2
6 Comments
 
LVL 11

Expert Comment

by:dougaug
ID: 26082975
Try this query:

select prod.product_name,
          prod.product_description,
          prod2.product_name,
          prod2.product_description
from #tblproducts prod inner join #tblsuggestedproducts sugprod on (prod.product_id = sugprod.product_id)
                       inner join #tblproducts prod2 on (prod2.product_id = sugprod.suggestedproduct_id)
0
 
LVL 11

Accepted Solution

by:
dougaug earned 2000 total points
ID: 26082992
Delete the '#' characters from the query before execute it:

select prod.product_name,
          prod.product_description,
          prod2.product_name,
          prod2.product_description
from tblproducts prod inner join tblsuggestedproducts sugprod on (prod.product_id = sugprod.product_id)
                       inner join tblproducts prod2 on (prod2.product_id = sugprod.suggestedproduct_id)
0
 
LVL 10

Expert Comment

by:joriszwaenepoel
ID: 26083000
SELECT T2.*, T3.*
FROM tblsuggestedproducts T1
INNER JOIN tblproducts T2 ON T1.productid = T2.ID
INNER JOIN tblproducts T3 ON T1.suggestedproductid = T3.ID
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

Author Comment

by:cartch2008
ID: 26083056
Brilliant dougag...now, what if I also wanted to join the products table to another table....ie:  I have a Vendor table.  tblproducts has a vendorid in it that links to the vendorid in the vendortable
0
 

Author Closing Comment

by:cartch2008
ID: 31667894
thanks...would like to know how I can join another table to tblproducts though
0
 
LVL 11

Expert Comment

by:dougaug
ID: 26083891
Try this:

select    prod.product_name,
          prod.product_description,
          vend.vendor_name,
          prod2.product_name,
          prod2.product_description,
          vend2.vendor_name
from tblproducts prod inner join tblsuggestedproducts sugprod on (prod.product_id = sugprod.product_id)
                       inner join tblproducts prod2 on (prod2.product_id = sugprod.suggestedproduct_id)
                       inner join tblvendor vend on (vend.vendor_id = prod.vendor_id)
                       inner join tblvendor vend2 on (vend2.vendor_id = prod2.vendor_id)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

810 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