473,403 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

selecting mutual results

Hey all, I'm probably just having a lax moment but I can't quite figure this
out. Say I have a table such as this:

friends

user friend
1 2
1 3
2 4
2 1

how could i select results where user and friend are the same but in
opposite rows (that is, in this example, user 1 and friend 2 (user 2 and
friend 1))?

Thanks :)
Jul 23 '05 #1
2 1326
Phillip Parr wrote:
how could i select results where user and friend are the same but in
opposite rows (that is, in this example, user 1 and friend 2 (user 2 and
friend 1))?


SELECT f1.user, f2.friend
FROM friends AS f1 INNER JOIN friends f2
ON (f1.user = f2.friend AND f1.friend = f2.user)
WHERE f1.user <= f1.friend

(the <= comparison at the end is to prevent pairs from appearing twice
in the result)

Regards,
Bill K.
Jul 23 '05 #2

"Bill Karwin" <bi**@karwin.com> wrote in message
news:d5********@enews3.newsguy.com...
Phillip Parr wrote:
how could i select results where user and friend are the same but in
opposite rows (that is, in this example, user 1 and friend 2 (user 2 and
friend 1))?


SELECT f1.user, f2.friend
FROM friends AS f1 INNER JOIN friends f2
ON (f1.user = f2.friend AND f1.friend = f2.user)
WHERE f1.user <= f1.friend

(the <= comparison at the end is to prevent pairs from appearing twice in
the result)

Regards,
Bill K.


Ah yes, Thanks! f2.friend in the select statement needs to be f2.user to get
the result I wanted :)

Phil
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Morten Gulbrandsen | last post by:
Hello Programmers, Thank you for the kind advice, I have another small problem with MySQL I'd like to investigate, All I get is this:=20 ERROR 1005 at line 47:=20 Can't create table...
0
by: Morten Gulbrandsen | last post by:
mysql> USE company; Database changed mysql> mysql> DROP TABLE IF EXISTS EMPLOYEE; -------------- DROP TABLE IF EXISTS EMPLOYEE -------------- Query OK, 0 rows affected (0.00 sec)
5
by: clusardi2k | last post by:
Hello, I have a assignment just thrown onto my desk. What is the easiest way to solve it? Below is a brief description of the task. There are multible programs which use the same library...
0
by: Support | last post by:
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40"> <head>...
2
by: Phillip Parr | last post by:
hah well I'm here again, guess I need more practice to do this all properly! I have a table, such as this: friends +--------+----------+ | userid | friendid | +--------+----------+ | ...
2
by: icoba | last post by:
I have the following code: A.py ==== from B import B class R: def __str__(self): return "hello world"
0
by: Paul | last post by:
I have found a web service that provides stock quotes on a twenty minute delay for free. Does anyone know of a web service that provides the mutual fund prices, hopefully for the same price? Thanks.
3
by: joshsackett | last post by:
I am having a problem with indexes on specific tables. For some reason a query that runs against a view is not selecting the correct index on a table. I run the same query against the table...
1
by: illegal.prime | last post by:
So I have a container of objects that I don't want to iterate across when I'm modifying it. I.E. I lock on adds and deletes to the container - so that my traversals of it don't result in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.