Solved

SQL statement to join data from two tables

Posted on 2008-10-10
3
206 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now