Solved

SQL statement to join data from two tables

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

18 Experts available now in Live!

Get 1:1 Help Now