Solved

MYSQL Query with subquery or join

Posted on 2013-01-29
3
528 Views
Last Modified: 2013-01-29
i have a query which has several fields like relationshipid, userid1 and userid2

friendshipid|userid1|userid2
1|4|5
2|8|4
3|7|4
4|4|9

i get that data via select * from friendship where userid1=4 or userid2=4

in mycase userid = 4 is the one who initiated the friendship

what i need to do is find a way how i can get always the other userid like 5 in row one so i can query another table via subquery or a joint to get the name of the other party but not sure how to do this most effectively
0
Comment
Question by:AlexPonnath
3 Comments
 
LVL 24

Accepted Solution

by:
johanntagle earned 300 total points
Comment Utility
You can use this as your subquery

select if(userid1=4,userid2,userid1) from friendship where userid1=4 or userid2=4;

Basically if userid1 equals the userid being searched, then userid2 must be the "friend userid", else userid1 is the friend.

I'm thinking this will really be used as a subquery, not as part of a join.
0
 

Author Comment

by:AlexPonnath
Comment Utility
Yes that looks somewhat promissing but how could i use that to do a join, since i dont want 10 subqueries to get Name etc for that friend id..

lets say i want to join with usertable on the value from the query where usertable.userid = the userid from your query,
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 200 total points
Comment Utility
You can use exactly that expression in the join

On if(...) = USERID
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

728 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

11 Experts available now in Live!

Get 1:1 Help Now