Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.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/5/sql/87.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 两个相关表之间的查询_Mysql_Sql - Fatal编程技术网

Mysql 两个相关表之间的查询

Mysql 两个相关表之间的查询,mysql,sql,Mysql,Sql,我必须坐在桌子上。其中一个名为user\u profile,有一个名为user\u profile\u id的字段,另一个名为user\u friend的表有两个名为user\u profile1\u id和user\u profile2\u id的字段(它们与第一个表是FK) 我想检查user_profile中是否有任何用户不在user_friend表中,既不在user_profile 1_id中,也不在user_profile 2_id中。我应该使用什么查询 试试这个 SELECT * F

我必须坐在桌子上。其中一个名为
user\u profile
,有一个名为
user\u profile\u id
的字段,另一个名为
user\u friend
的表有两个名为
user\u profile1\u id
user\u profile2\u id
的字段(它们与第一个表是FK)

我想检查user_profile中是否有任何用户不在user_friend表中,既不在user_profile 1_id中,也不在user_profile 2_id中。我应该使用什么查询

试试这个


SELECT * FROM users
LEFT JOIN friends f1 ON (users.id = friends.uid)
LEFT JOIN friends f2 ON (users.id = friends.fid)
WHERE f1.uid IS NULL AND f2.uid IS NULL
试试这个


SELECT * FROM users
LEFT JOIN friends f1 ON (users.id = friends.uid)
LEFT JOIN friends f2 ON (users.id = friends.fid)
WHERE f1.uid IS NULL AND f2.uid IS NULL
Try(这是MSSQL,但在MySQL中应该类似)

Try(这是MSSQL,但在MySQL中应该类似)

尝试:

可能有更高效的查询,但这应该可以完成这项工作。

试试:


可能有更高效的查询,但这应该可以完成这项工作。

首先,我建议您在user\u profile\u表中使用循环外键,其中外键引用回同一表的主键。这样,您就不用为用户之间的关系创建另一个表了

但是,使用您的设计,您可以使用以下查询:

SELECT user_profile_id 
FROM user_profile A
 WHERE 
  NOT EXISTS(
      SELECT * FROM user_friends B 
       WHERE B.user_profile1_id=A.user_profile_id OR 
             B.user_profile2_id=A.user_profile_id
            )

首先,我建议您在user_profile_表中使用循环外键,其中外键引用回同一表的主键。这样,您就不用为用户之间的关系创建另一个表了

但是,使用您的设计,您可以使用以下查询:

SELECT user_profile_id 
FROM user_profile A
 WHERE 
  NOT EXISTS(
      SELECT * FROM user_friends B 
       WHERE B.user_profile1_id=A.user_profile_id OR 
             B.user_profile2_id=A.user_profile_id
            )

有没有办法不加入!?因为用户朋友表有2000万条记录!有没有办法不加入!?因为用户朋友表有2000万条记录!您确定它同时检查了用户\u profile\u id、用户\u profile1\u id和用户\u profile2\u id!?我的意思是,它会检查一张记录中的两张吗!?您确定它同时检查了用户\u profile\u id、用户\u profile1\u id和用户\u profile2\u id!?我的意思是,它会检查一张记录中的两张吗!?有没有办法不加入!?因为用户朋友表有2000万条记录!类似这样的
如果(!'SELECT COUNT(*)FROM friends WHERE(uid=uidVal和fid=fidVal)或(uid=fidVal和fid=uidVal){do stuff}
有没有不加入的方法!?因为用户朋友表有2000万条记录!类似这样的
if(!'SELECT COUNT(*)FROM friends,其中(uid=uidVal和fid=fidVal)或(uid=fidVal和fid=uidVal){do stuff}
一个右括号是多余的。内部的
WHERE
子句也可以重写为
WHERE A.user\u profile\u id IN(B.user\u profile1\u id,B.user\u profile2\u id)
,但这可能无关紧要。@Andriy M:是的,你是对的。谢谢你的提醒。我修正了:)一个右括号是多余的。内部的
WHERE
子句也可以重写为
WHERE A.user\u profile\u id IN(B.user\u profile1\u id,B.user\u profile2\u id)
,但这可能无关紧要。@Andriy M:是的,你是对的。谢谢你的提醒。我修好了:)