Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.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连接正确的用户名_Mysql_Sql_Database - Fatal编程技术网

Mysql SQL连接正确的用户名

Mysql SQL连接正确的用户名,mysql,sql,database,Mysql,Sql,Database,假设我有以下表格 User __________ id username email FriendGame __________ id useroneid usertwoid status 我希望获得当前用户参与的游戏,因此我会这样做: SELECT * FROM FriendGame WHERE useroneid=1663702020516206 OR usertwoid=1663702020516206 AND STATUS =1; 这很好。现在我想加入用户名,但只针对不是提供的用户

假设我有以下表格

User
__________
id
username
email

FriendGame
__________
id
useroneid
usertwoid
status
我希望获得当前用户参与的游戏,因此我会这样做:

SELECT *
FROM FriendGame
WHERE useroneid=1663702020516206
OR usertwoid=1663702020516206
AND STATUS =1;

这很好。现在我想加入用户名,但只针对不是提供的用户(1663702020516206)的用户,因为在FriendGame中,给定的用户以useroneid或usertwoid的形式存在。

您几乎可以将逻辑直接转换为
on
子句:

SELECT fg.*
FROM FriendGame fg JOIN
     User u
     ON (fg.useroneid = 1663702020516206 and fg.usertwoid = u.id) or
        (fg.usertwoid = 1663702020516206 and fg.useroneid = u.id) 
WHERE 1663702020516206 in (fg.useroneid, fg.usertwoid) AND
      STATUS = 1;

实际上,
where
子句对于获得正确的结果集不是必需的,但我认为它使查询的意图更加清晰。

对不起,我发布了一个编辑,但我想我误解了@brux所说的“只有非提供用户的用户”——请拒绝我的编辑