Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Query with Multiple table for conditions

Tags:

sql

I have an issue with a table I am trying to produce in Visual Studio 2010. I am not quite sure how to formulate my SQL query that forms my datasource.

Basically I have two tables. One of them contains picture filenames and has two columns, picturefile (PK) and username. The second table contains three username columns: the first colum defines a username,the second one of the username's friends so as to form friend relationships and the third column is a RelationshipID.

I would like to have my query produce a table that shows all the pictures from the pictures table that are owned by the current user's friends. i have the parameter @currentuser which can be used So this would be a Your Friend's Pictures Table.

The attempt I had, bear in mind I am very much a beginner, is this:

SELECT picturefile, username
FROM     pictures_table1
WHERE (username = (SELECT User2 FROM friendslist_table1 WHERE     friendslist_table1.Username1 = @currentuser AND friendslist.Username2 <> @currentuser))

It gives an error saying the subquery cannot return mulitple values.

Thank you.

like image 436
user1824836 Avatar asked Nov 30 '12 04:11

user1824836


2 Answers

SELECT f.friendname,p.picturefile from pictures_table1 p
INNER JOIN friendslist_table1 f
ON p.username = f.username
WHERE f.username = @currentuser
like image 184
Mudassir Hasan Avatar answered Oct 14 '22 01:10

Mudassir Hasan


Please look at JOIN function, it does exactly what you want.

like image 2
sashkello Avatar answered Oct 14 '22 00:10

sashkello