Solved

MYSQL Query with subquery or join

Posted on 2013-01-29
3
535 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
[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
3 Comments
 
LVL 24

Accepted Solution

by:
johanntagle earned 300 total points
ID: 38834014
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
ID: 38834085
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 143

Assisted Solution

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

On if(...) = USERID
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this article, we’ll look at how to deploy ProxySQL.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

621 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