Solved

SQL statement to join data from two tables

Posted on 2008-10-10
3
209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

739 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