MySQL如何从友谊表中选择朋友? [英] MySQL How to select friends from a friendship table?

查看:87
本文介绍了MySQL如何从友谊表中选择朋友?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于存储友谊的简单数据库,只有两列person_a,person_b,表示person_a和person_b是朋友。在这样的大清单中,我应该问什么查询以说出‘sam’的所有朋友。谢谢。

Hi I have a simple database for storing friendship, with just two columns person_a, person_b , denoting that person_a and person_b are friends. From a big list like this what query should I run to get say get all friends of 'sam'. Thank you.

推荐答案

使用UNION:

SELECT person_b
FROM friendship
WHERE person_a = 'sam'
UNION
SELECT person_a
FROM friendship
WHERE person_b = 'sam'

这篇关于MySQL如何从友谊表中选择朋友?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆