- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这里是 MySQL 5.6。我有以下表格:
DESCRIBE profiles;
+----------------------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------------+---------------------+------+-----+---------+----------------+
| profile_id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| profile_given_name | varchar(100) | YES | MUL | NULL | |
| profile_surname | varchar(100) | YES | MUL | NULL | |
+----------------------------+---------------------+------+-----+---------+----------------+
describe friendships;
+----------------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+---------------------+------+-----+---------+----------------+
| friendship_id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| requester_profile_id | bigint(20) unsigned | NO | MUL | NULL | |
| recipient_profile_id | bigint(20) unsigned | NO | MUL | NULL | |
+----------------------+---------------------+------+-----+---------+----------------+
profiles
表代表系统的用户,friendships
代表多对多关系,其中用户与其他用户是 friend 。当用户/配置文件向用户发送“好友请求”时,他们被视为好友的请求者
。相反,那些从其他人那里收到 friend 请求的人是收件人
。
我正在尝试编写一个(看似)简单的符合 ANSI 标准的 SELECT
,它告诉我特定配置文件的所有配置文件是好友,无论它们是否是 请求者
或友谊的接收者
。
用一些数据设置数据库:
INSERT INTO friendships (
friendship_ref_id,
requester_profile_id,
recipient_profile_id
) VALUES (
'01234',
2,
1
), (
'67890',
1,
3
), (
'78901',
1,
4
), (
'89012',
2,
3
), (
'90123',
3,
4
);
那么我迄今为止最好的尝试:
SELECT f.requester_profile_id,
f.recipient_profile_id
FROM profiles p
INNER JOIN friendships f
ON (
f.requester_profile_id = p.profile_id
OR
f.recipient_profile_id = p.profile_id
)
WHERE p.profile_id = 1;
当我运行它时,我得到:
+----------------------+----------------------+
| requester_profile_id | recipient_profile_id |
+----------------------+----------------------+
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
| 2 | 1 |
+----------------------+----------------------+
但我真正想要的是由 profile_id = 1
的所有 DISTINCT
配置文件 ID(类似的请求者和接收者)组成的一个列是 friend ,比如:
+----------------------+
| profile_friends |
+----------------------+
| 2 |
| 3 |
| 4 |
+----------------------+
...因为 profile_id = 1
是 profile_id IN { 2, 3, 4 }
等的 friend
我在这里缺少什么才能使查询正常工作?
最佳答案
这里有两个选项。
第一个使用 CASE 来决定每个结果使用哪一列:
SELECT
CASE WHEN f.requester_profile_id = p.profile_id THEN f.recipient_profile_id
ELSE f.requester_profile_id END as profile_friends
FROM profiles p
INNER JOIN friendships f
/* Original ON clause would still work, but I prefer the briefer syntax */
ON p.profile_id IN (f.requester_profile_id,f.recipient_profile_id)
WHERE p.profile_id = 1;
第二个 UNION 将两个 SELECT 语句组合在一起,其中一个检查请求者,另一个检查接收者:
SELECT f.recipient_profile_id as profile_friends
FROM profiles p
INNER JOIN friendships f
ON f.requester_profile_id = p.profile_id
WHERE p.profile_id = 1
UNION
SELECT f.requester_profile_id
FROM profiles p
INNER JOIN friendships f
ON f.recipient_profile_id = p.profile_id
WHERE p.profile_id = 1;
关于MySQL:从 INNER JOIN 内部分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48550581/
class Outer { class Inner { } } public class Demo { public static void main(
我在我的 PC 上使用 bitnami WAMP 创建了一个 mysql 服务器,并使用流畅的 SQL 创建了两个表 table_a 和 table_b: CREATE TABLE `table_a`
select sld.linkid,sld.accept,scd.catid,scd.catname,scd.caturl,scd1.parentcatid from sound_li
为什么在实例化通用外部类(连同内部类)时使用菱形运算符会在代码段 2 中产生错误,而代码段 1 却完全没问题? 我知道稀有类型是被禁止的,但我的情况不是稀有类型——在稀有类型中,外部和内部都是通用的,
这不是那么重要(请注意),但它是一个语法特殊性,它是关于可移植性的,我没有找到引用可靠资源或规范的问题(或答案),所以我很好奇: 省略 INNER 是否安全关键字,如果我明确想要 INNER JOIN
同一查询的这两个版本之间的性能有什么区别吗? --Version 1 SELECT p.Name, s.OrderQty FROM Product p INNER JOIN SalesOrderDet
阅读 Doctrine 2 文档时 at this page我可以看到他们有两种方法来指定内部连接的条件: // Example - $qb->expr()->innerJoin('u.Group',
我想知道我怎么能直接从外部类型到达内部类,就好像它是一个静态成员一样: public class Hello { public class UnderHello { v
我有一个这样的界面: public interface SuperInterface { public interface SubInterface { public void
假设有两个表: students (student_id INT, class_id INT); instructors (instructor_id INT, class_id INT); 这两者之
有表 A 和表 B。我想将这些表连接到两列,但仅限于表 A 的选定行。 查询场景: SELECT B.* FROM B INNER JOIN (SELECT * FROM A WHERE A.COLU
今天我了解了SQL Server中的一个东西,叫做INNER LOOP JOIN . 这是什么意思? (谷歌没有提供帮助……或者我应该说……有关它的博客文章有点……技术性,让我大吃一惊)。 此外,在哪
我开始学习 SQL。而且我发现我们经常可以在 JOIN 或内部选择语句的帮助下获得相同的结果。 问题 1(广泛):JOIN 在哪里比内部选择更快,反之亦然? 问题 2(窄):您能解释一下是什么导致了以
我真的很努力地完成这项任务,因此非常感谢任何帮助或指导。 表格: module_uploads +--------------------------+
仅当连接表中存在记录时,我才尝试根据连接表中的条件从用户表中选择一行。如果记录不存在,我想返回该行。我只想在存在 INNER JOIN 记录且 contact_blocked != 1 时不返回该行。
我有一个包含字符的数据库。由于角色的名称不仅在名称与角色分开时才使用。由于有 Male 和 Female 字符,我将它们保存在不同的表中: tbl_player, tbl_maleName, tbl_
[决定如何表述问题并不容易...] 这是一个 example jsfiddle . 紫色的 div ('#inner') 高 1000 像素。 如何制作红色 div ('#out') 的滚动条 - 控
我正在为 wordpress 中的帖子开发一个过滤器,它使用来自 postmeta 的数据:我的日期过滤器正在运行并给我这个查询: 工作查询结果: SELECT SQL_CALC_FOUND_ROWS
我有使用引导类并动态添加 带有引导类和 clearfix 的元素 var li = $("").addClass("list-group-item clearfix"); var div = $(
我已经为 Test_flow 创建了简单的测试套件,但是当我尝试运行该流程时出现错误。 java.lang.RuntimeException: org.mule.api.config.Configur
我是一名优秀的程序员,十分优秀!