Solved

SQL statement to join data from two tables

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sql Stored Procedure field variable 17 38
SQL Syntax 5 39
export sql results to csv 6 41
SQL Server - Set Value of Multiple Fields in One Query 10 26
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…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

820 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