Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php MYSQL-随机选择一行,其中两个用户没有相互跟踪?_Php_Mysql_Database_Random - Fatal编程技术网

Php MYSQL-随机选择一行,其中两个用户没有相互跟踪?

Php MYSQL-随机选择一行,其中两个用户没有相互跟踪?,php,mysql,database,random,Php,Mysql,Database,Random,我正在开发一个应用程序,旨在向用户显示一个从列表中随机选择的用户,他/她可能希望在他们尚未相互跟踪的地方跟踪 我有两个表一个表有一个用户列表,第二个表有两列,显示“跟随”关系 表1用户用户id、instagram id、加入日期 表2用户关注度-instagram\u id\u 1、instagram\u id\u 2 我只是想知道是否有人能给我指出正确的方向,告诉我如何创建连接并选择一个随机的行,其中两个用户没有相互跟踪?我试过左键,但到目前为止运气不好 这是到目前为止的代码 SELECT *

我正在开发一个应用程序,旨在向用户显示一个从列表中随机选择的用户,他/她可能希望在他们尚未相互跟踪的地方跟踪

我有两个表一个表有一个用户列表,第二个表有两列,显示“跟随”关系

表1用户用户id、instagram id、加入日期

表2用户关注度-instagram\u id\u 1、instagram\u id\u 2

我只是想知道是否有人能给我指出正确的方向,告诉我如何创建连接并选择一个随机的行,其中两个用户没有相互跟踪?我试过左键,但到目前为止运气不好

这是到目前为止的代码

SELECT * 
FROM users
INNER JOIN  `user_follows` ON users.instagram_id = user_follows.instagram_id
WHERE user_follows.instagram_id !=  'insta123'

感谢您的帮助。

我建议您进行子查询,以获取您的关注者列表。然后用户使用左连接,并通过测试NULL找到那些不是跟随者的人

SELECT users.* from users
left join  
(
    SELECT 
        users_follows.instagram_id_2 as 'instagram_id'
    from users
    inner join users_follows on users.instagram_id=users_follows.instagram_id_1
    where users.instagram_id = 'insta123'
) as followers on users.instagram_id = followers.instagram_id
where followers.instagram_id is null
and users.instagram_id != 'insta123' # you can't follow yourself

我已经用你的结构的复制品测试过了,效果很好。

我建议你做一个子查询,以获得你的追随者列表。然后用户使用左连接,并通过测试NULL找到那些不是跟随者的人

SELECT users.* from users
left join  
(
    SELECT 
        users_follows.instagram_id_2 as 'instagram_id'
    from users
    inner join users_follows on users.instagram_id=users_follows.instagram_id_1
    where users.instagram_id = 'insta123'
) as followers on users.instagram_id = followers.instagram_id
where followers.instagram_id is null
and users.instagram_id != 'insta123' # you can't follow yourself

我已经用你的结构的副本测试了它,它工作得很好。

请留下一些代码,你到目前为止拥有的代码。你的意思是两个用户没有相互跟踪还是两个用户都没有相互跟踪?我想你会想要一个
外部连接
。是的,理想情况是两个用户没有相互跟踪。所以我有用户A的id,我需要找到另一个用户,这两个用户没有相互跟踪。请留下一些代码,到目前为止,您拥有的是什么。您是指两个用户没有相互跟踪还是两个用户都没有相互跟踪?我想您可能需要一个
外部连接
。是的,理想情况下是两个用户没有相互跟踪。因此,我有用户A的id,我需要找到另一个用户,这两个用户没有相互跟踪。