Mysql 将限制结果联接到联接表,而不使用子选择
我有一个连接到表的查询(user with friends user),我想将第二个连接的表限制为组的前n行。 我在stack和这篇文章上读了几个帖子: 我想知道:不使用子选择有什么变化吗 以下是一个数据集示例:Mysql 将限制结果联接到联接表,而不使用子选择,mysql,sql,join,left-join,Mysql,Sql,Join,Left Join,我有一个连接到表的查询(user with friends user),我想将第二个连接的表限制为组的前n行。 我在stack和这篇文章上读了几个帖子: 我想知道:不使用子选择有什么变化吗 以下是一个数据集示例: +----+------------+------------+------------+----------+-----------------+---------------------+----------+-------------------+------+ | id |
+----+------------+------------+------------+----------+-----------------+---------------------+----------+-------------------+------+
| id | created_at | updated_at | deleted_at | status | username | mail | password | recovery_password | pin |
+----+------------+------------+------------+----------+-----------------+---------------------+----------+-------------------+------+
| 1 | 1412441692 | NULL | NULL | enabled | first.first | first@first.com | 111111 | NULL | 1111 |
| 2 | 1412441692 | NULL | NULL | enabled | second.second | second@second.com | 222222 | NULL | 2222 |
| 3 | 1412441692 | NULL | NULL | disabled | third.third | third@third.com | 333333 | NULL | 3333 |
| 4 | 1412441692 | NULL | NULL | enabled | fourth.fourth | fourth@fourth.com | 444444 | NULL | 4444 |
| 5 | 1412441692 | NULL | NULL | disabled | fifth.fifth | fifth@fifth.com | 555555 | NULL | 5555 |
| 6 | 1412441692 | NULL | NULL | enabled | sixth.sixth | sixth@sixth.com | 666666 | NULL | 6666 |
| 7 | 1412441692 | NULL | NULL | disabled | seventh.seventh | seventh@seventh.com | 777777 | NULL | 7777 |
+----+------------+------------+------------+----------+-----------------+---------------------+----------+-------------------+------+
+-----+--------+---------+-----+--------+------------+-----------+
| id1 | otype1 | atype | id2 | otype2 | pushed_at | popped_at |
+-----+--------+---------+-----+--------+------------+-----------+
| 1 | user | friends | 2 | user | 1412441692 | NULL |
| 1 | user | friends | 3 | user | 1412441692 | NULL |
| 2 | user | friends | 1 | user | 1412441692 | NULL |
| 2 | user | friends | 6 | user | 1412441692 | NULL |
| 4 | user | friends | 3 | user | 1412441692 | NULL |
| 6 | user | friends | 2 | user | 1412441692 | NULL |
| 6 | user | friends | 7 | user | 1412441692 | NULL |
+-----+--------+---------+-----+--------+------------+-----------+
这是我现在使用的查询
SELECT `t1`.`id` AS `id1`, t2.* FROM `User` AS `t1`, `user` AS `t2`, `association` AS `a` WHERE ( t1.id = a.id1 ) AND ( t2.id = a.id2 ) AND ( a.otype1 = "user" ) AND ( a.otype2 = "user") AND ( a.atype = "friends" )
+-----+----+------------+------------+------------+----------+-----------------+---------------------+----------+-------------------+------+
| id1 | id | created_at | updated_at | deleted_at | status | username | mail | password | recovery_password | pin |
+-----+----+------------+------------+------------+----------+-----------------+---------------------+----------+-------------------+------+
| 1 | 2 | 1412441692 | NULL | NULL | enabled | second.second | second@second.com | 222222 | NULL | 2222 |
| 1 | 3 | 1412441692 | NULL | NULL | disabled | third.third | third@third.com | 333333 | NULL | 3333 |
| 2 | 1 | 1412441692 | NULL | NULL | enabled | first.first | first@first.com | 111111 | NULL | 1111 |
| 2 | 6 | 1412441692 | NULL | NULL | enabled | sixth.sixth | sixth@sixth.com | 666666 | NULL | 6666 |
| 4 | 3 | 1412441692 | NULL | NULL | disabled | third.third | third@third.com | 333333 | NULL | 3333 |
| 6 | 2 | 1412441692 | NULL | NULL | enabled | second.second | second@second.com | 222222 | NULL | 2222 |
| 6 | 7 | 1412441692 | NULL | NULL | disabled | seventh.seventh | seventh@seventh.com | 777777 | NULL | 7777 |
+-----+----+------------+------------+------------+----------+-----------------+---------------------+----------+-------------------+------+
但我想得到:
+-----+----+------------+------------+------------+----------+-----------------+---------------------+----------+-------------------+------+
| id1 | id | created_at | updated_at | deleted_at | status | username | mail | password | recovery_password | pin |
+-----+----+------------+------------+------------+----------+-----------------+---------------------+----------+-------------------+------+
| 1 | 2 | 1412441692 | NULL | NULL | enabled | second.second | second@second.com | 222222 | NULL | 2222 |
| 2 | 1 | 1412441692 | NULL | NULL | enabled | first.first | first@first.com | 111111 | NULL | 1111 |
| 4 | 3 | 1412441692 | NULL | NULL | disabled | third.third | third@third.com | 333333 | NULL | 3333 |
| 6 | 2 | 1412441692 | NULL | NULL | enabled | second.second | second@second.com | 222222 | NULL | 2222 |
+-----+----+------------+------------+------------+----------+-----------------+---------------------+----------+-------------------+------+
是的,您可以使用变量。不过,首先,您应该更像这样编写查询:
SELECT u1.id AS id1, u2.*
FROM association a JOIN
User u1
on u1.id = a.id1 and a.otype1 = 'user' JOIN
`user` u2
ON u2.id = a.id2 and a.otype2 = 'user'
WHERE a.atype = 'friends'
注意(1)显式join
语法的使用;(2) 字符串常量的单引号;(3) 表名缩写的别名;(4)尽量少使用反勾号(这只会使查询更难阅读)
SELECT uu.*
FROM (SELECT u1.id AS id1, u2.*,
(@rn := if(@id1 = id1, @rn + 1,
if(@id1 := id1, 1, 1)
)
) rn
FROM association a JOIN
User u1
on u1.id = a.id1 and a.otype1 = 'user' JOIN
`user` u2
ON u2.id = a.id2 and a.otype2 = 'user' CROSS JOIN
(SELECT @id1 := 0, @rn := 0) vars
WHERE a.atype = 'friends'
ORDER BY u1.id
) uu
WHERE rn = 1;
ehi gordon,首先谢谢。我正试图弄清楚你的查询。1)那些子查询不是吗?2) 第一个选择是一个有编号行的技巧吗?3) 请你进一步解释一下第一个选择和交叉连接选择好吗?我真的非常感谢您的帮助。这里有一个很大的子查询,并通过
id
对结果进行排序。然后使用变量枚举每个id
的行。交叉连接
和后续子查询只是在查询中定义变量。