Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Query using 2 tables

Posted on 2007-11-16
4
Medium Priority
?
179 Views
Last Modified: 2010-04-15
How to write a query to select 3 columns from 2 different tables?
For example there is USERID ,PRODUCTID in table A, and ID (which holds the same value as ProductID in tableA),ProductName in a table b.Now I want to show that this particular user  is having specific productid and product name.
0
Comment
Question by:onebite2
  • 2
3 Comments
 
LVL 5

Expert Comment

by:gigglick
ID: 20299431
select distinct USERID , PRODUCTID ,ProductName  from tableA inner join tableB on tableA.PRODUCTID  = tableB.ID

for a specific USERID:
select distinct USERID , PRODUCTID ,ProductName  from tableA inner join tableB on tableA.PRODUCTID  = tableB.ID where USERID = theuserid
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20324894
you can prefix the columns based on which table they came from, i.e.

select TableA.USERID, TableA.PRODUCTID, TableB.ID, TableB.ProductName
from TableA
 inner join TableB on TableA.PRODUCTID = TableB.ID
0
 
LVL 25

Accepted Solution

by:
imitchie earned 1500 total points
ID: 20495518
I can't see where the DISTINCT operator figures in the solution at all
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

783 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