Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.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
Mysql Sql连接3个表_Mysql - Fatal编程技术网

Mysql Sql连接3个表

Mysql Sql连接3个表,mysql,Mysql,我一直在努力生成好的SQL。 它是应用程序的关系部分。 我有3个表:users'id',relationship'user\u id',member\u id',relationship\u block'user\u id',blocked\u member\u id' 我想获得属于用户id为user_且未被阻止的用户的所有成员 “关系”表中第一个表“用户”的记录,但不在“关系”表中。 首先我可以使用JOIN,但是我想删除那些被阻止的 塔克斯 编辑:在这里找到了关于它的好信息:作为旁注,不要选择

我一直在努力生成好的SQL。 它是应用程序的关系部分。 我有3个表:users'id',relationship'user\u id',member\u id',relationship\u block'user\u id',blocked\u member\u id'

我想获得属于用户id为user_且未被阻止的用户的所有成员

“关系”表中第一个表“用户”的记录,但不在“关系”表中。 首先我可以使用JOIN,但是我想删除那些被阻止的

塔克斯


编辑:在这里找到了关于它的好信息:

作为旁注,不要选择*。。。这只是为了说明目的!我突然想到,这可以解释为需要三个查询才能完成。你没有。前两个只是展示了如何构建所需查询的思维过程。我可能做错了什么,但这似乎不起作用。可能是在关系块表中只有一行或两行块,但有更多的用户行。更简单的解释是,我需要返回关系表中的所有成员关系块表中不存在的所有成员ID。因此,例如,如果relationship_blocks表中没有行,它将返回所有用户。如果关系块表中有1行的“user\u id”=1,“blocked\u member\u id”=4,则它将返回关系表中显示为member\u id的所有用户,id为4的用户除外。谢谢您的解决方案。这现在就行了。虽然就我的应用程序的性质而言,我不能将2个选择组合成一个,所以我将使用下面的解决方案。非常感谢。对于其他正在寻找同样东西的人,我在这里找到了好信息:作为旁注,不要选择*。。。这只是为了说明目的!我突然想到,这可以解释为需要三个查询才能完成。你没有。前两个只是展示了如何构建所需查询的思维过程。我可能做错了什么,但这似乎不起作用。可能是在关系块表中只有一行或两行块,但有更多的用户行。更简单的解释是,我需要返回关系表中的所有成员关系块表中不存在的所有成员ID。因此,例如,如果relationship_blocks表中没有行,它将返回所有用户。如果关系块表中有1行的“user\u id”=1,“blocked\u member\u id”=4,则它将返回关系表中显示为member\u id的所有用户,id为4的用户除外。谢谢您的解决方案。这现在就行了。虽然就我的应用程序的性质而言,我不能将2个选择组合成一个,所以我将使用下面的解决方案。非常感谢。对于其他正在寻找同样东西的人,我在这里找到了很好的信息:
/* this would get the user */
SELECT *
FROM users
WHERE id = $ID

/* build on this to get relationships */
SELECT *
FROM users u
JOIN relationship r ON r.user_id = u.id
WHERE u.id = $ID

/* build on this to get not blocked */
SELECT *
FROM users u
JOIN relationship r ON r.user_id = u.id
JOIN relationship_block b ON b.user_id = u.id
WHERE u.id = $ID
  AND r.member_ID <> b.blocked_member_id

/* get all users that NO ONE has blocked */
/* this means if there exists a record b such that b.blocked_member_id
   equals the user X has blocked user Y, do not include user Y.
   By extension, if X and Y are fierce enemies and have blocked eachother,
   neither would get returned by the query */
SELECT *
FROM users u
JOIN relationship r ON r.id = u.id
WHERE NOT EXISTS ( SELECT null
                   FROM relationship_block rb
                   WHERE rb.blocked_member_id = u.id
                 )
/* This runs two queries at once. The inner query says "I'm not getting any
   columns, because I don't care about the actual data, I just to get all
   records where someone has blocked the user I'm currently looking for".
   Then you select all users where that isn't true. For good speed, this would
   require an index on relationship_block.blocked_member_id */
select *
    from users u
        inner join relationship r
            on u.user_id = r.user_id
        left join relationship_block rb
            on r.user_id = rb.user_id
                and r.member_id = rb.blocked_member_id
    where rb.user_id is null