Solved

SQL statement to join data from two tables

Posted on 2008-10-10
3
207 Views
Last Modified: 2010-03-20
Hi,

I have two tables:

     users
    ------------------------
    | id  | username    |
    ------------------------
       1       john
       2       frank  
       3       steve
       4       bob


     friends
     ----------------------------------
    | id  | id_user1  |  id_user2 |
    -----------------------------------
       1          2               1
       2          2               4

I want to write a statement which will give me a listing of all ids and usernames from the users table give a user ID from the friends table.

So in the above example, I want to get a listing of frank's friends' ids and usernames:

      1, john
      4, bob

I guess this should use some sort of inner join, but im not sure how to do it?

Thanks
0
Comment
Question by:DJ_AM_Juicebox
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
Binuth earned 500 total points
ID: 22692624
try this
SELECT 
	 u.id
	,u.username
FROM
	users u
INNER JOIN friends f 
	ON u.id = f.id_user2
WHERE
	f.id_user1 = 2

Open in new window

0
 
LVL 3

Expert Comment

by:3abqari
ID: 22692638
It depends on what your criteria of search is....

Are you using the username or the userID?

WHERE username = 'frank'

or

WHERE id = 2
0
 
LVL 3

Expert Comment

by:3abqari
ID: 22692647
SELECT u.id, u.username
FROM friends f
INNER JOIN users u
ON f.id_user2 = u.id
WHERE f.id_user1 = '2'

or

SELECT u.id, u.username
FROM friends f
INNER JOIN users u
on f.id_user2 = u.id
INNER JOIN users x
ON f.id_user1 = x.id
WHERE x.username = 'frank'
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS SQL - Rotating Values in SQL 9 65
Update data using formula 22 25
VB.net and sql server 4 35
Have a conversion issue with varchar to int in a SQL: Query. 1 29
In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

770 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