sql select 来自 table X 且未在 table Y 组合的所有用户
sql select all users from table X which are not in comination on table Y
我是 sql 的新手,当用户不在 table Y 时,我正在尝试通过玩家 ID 和世界 ID 的组合从 table X 获取数据并且播放器访问权限为 2。
让我再解释一下:
Table X(用户 table)
+-----------+----------+------------+
| uid | access | more data |
+-----------+----------+------------+
| 1 | 2 | .... |
| 2 | 1 | .... |
| 3 | 2 | .... |
+-----------+----------+------------+
Table Y(世界)
+-----------+-----------+
| userUuid | worldUuid |
+-----------+-----------+
| 1 | 1 |
| 2 | 2 |
| 3 | 2 |
+-----------+-----------+
当我想获取所有我仍然可以添加到世界 1 的用户时,我想从用户 3 获取用户信息。
用户 1 已经在世界 1 中,用户 2 没有访问级别 2,用户 3 不在世界 1 中但有访问级别 2。
我正在使用 medoo,这是我目前的声明:
$database->select("User", [
"[>]UserInWorld" => ["uid" => "userUid"]
], [
"uid",
"displayname",
"surname",
"email"
], [
"AND" => [
"worldUuid[!]" => $worldUuid,
"access" => 2
]
]);
worldUuid 将是我想让用户添加的世界。
当使用 ->debug() 时,查询如下所示:
SELECT "uid","displayname","surname","email"
FROM "User"
LEFT JOIN "UserInWorld" ON "User"."uid" = "UserInWorld"."userUid"
WHERE "worldUuid" != '4dafb8c0-57234ff2-03eb-af7f7a5e'
AND "access" = 2
编辑:我在下面发布了一个使用 medoo 的解决方案
如果我没理解错的话,你应该可以这样做:
SELECT
uid,
displayname,
surname,
email
FROM
User
LEFT JOIN UserInWorld ON User.uid = UserInWorld.userUid AND worldUuid = 1
INNER JOIN (
SELECT DISTINCT
userUid
from
UserInWorld
WHERE
worldUuid != 1
) AS InOtherWorld ON InOtherWorld.userUid = User.uid
WHERE
access = 2
AND UserInWorld.userUid IS NULL
左联接将尽可能连接世界上的人们,然后 UserInWorld.userUid IS NULL
将有效地将其分解为不在世界上的人们。
你说:
am trying to get data from table X when the user is not in table Y with the combination of player id and world id AND the player access is 2
如果我理解应该是:
select * from X where X.access = 2 and X.uid not in (
select Y.userUuid from Y
)
睡了一夜好觉后,我想出了如何使用 medoo 做到这一点 class
$database->select("User", [
"[>]UserInWorld" => ["uid" => "userUid"]
], [
"uid",
"displayname",
"surname",
"email"
], [
"AND" => [
"OR" => [
"worldUuid[!]" => [$worldUuid],
"worldUuid" => NULL
],
"access" => 2
],
"GROUP" => "uid"
]);
我想要 select 用户的 $worldUuid 世界。
这将生成以下 sql 语句:
SELECT "uid","displayname","surname","email" FROM "User"
LEFT JOIN "UserInWorld" ON "User"."uid" = "UserInWorld"."userUid"
WHERE ("worldUuid" NOT IN ('1') OR "worldUuid" IS NULL)
AND "access" = 2
GROUP BY "uid"
这将 select 所有(唯一)用户,他们还没有世界,或者在我正在获取用户的世界中,并且他们具有访问级别 2
我是 sql 的新手,当用户不在 table Y 时,我正在尝试通过玩家 ID 和世界 ID 的组合从 table X 获取数据并且播放器访问权限为 2。
让我再解释一下:
Table X(用户 table)
+-----------+----------+------------+
| uid | access | more data |
+-----------+----------+------------+
| 1 | 2 | .... |
| 2 | 1 | .... |
| 3 | 2 | .... |
+-----------+----------+------------+
Table Y(世界)
+-----------+-----------+
| userUuid | worldUuid |
+-----------+-----------+
| 1 | 1 |
| 2 | 2 |
| 3 | 2 |
+-----------+-----------+
当我想获取所有我仍然可以添加到世界 1 的用户时,我想从用户 3 获取用户信息。
用户 1 已经在世界 1 中,用户 2 没有访问级别 2,用户 3 不在世界 1 中但有访问级别 2。
我正在使用 medoo,这是我目前的声明:
$database->select("User", [
"[>]UserInWorld" => ["uid" => "userUid"]
], [
"uid",
"displayname",
"surname",
"email"
], [
"AND" => [
"worldUuid[!]" => $worldUuid,
"access" => 2
]
]);
worldUuid 将是我想让用户添加的世界。
当使用 ->debug() 时,查询如下所示:
SELECT "uid","displayname","surname","email"
FROM "User"
LEFT JOIN "UserInWorld" ON "User"."uid" = "UserInWorld"."userUid"
WHERE "worldUuid" != '4dafb8c0-57234ff2-03eb-af7f7a5e'
AND "access" = 2
编辑:我在下面发布了一个使用 medoo 的解决方案
如果我没理解错的话,你应该可以这样做:
SELECT
uid,
displayname,
surname,
email
FROM
User
LEFT JOIN UserInWorld ON User.uid = UserInWorld.userUid AND worldUuid = 1
INNER JOIN (
SELECT DISTINCT
userUid
from
UserInWorld
WHERE
worldUuid != 1
) AS InOtherWorld ON InOtherWorld.userUid = User.uid
WHERE
access = 2
AND UserInWorld.userUid IS NULL
左联接将尽可能连接世界上的人们,然后 UserInWorld.userUid IS NULL
将有效地将其分解为不在世界上的人们。
你说:
am trying to get data from table X when the user is not in table Y with the combination of player id and world id AND the player access is 2
如果我理解应该是:
select * from X where X.access = 2 and X.uid not in (
select Y.userUuid from Y
)
睡了一夜好觉后,我想出了如何使用 medoo 做到这一点 class
$database->select("User", [
"[>]UserInWorld" => ["uid" => "userUid"]
], [
"uid",
"displayname",
"surname",
"email"
], [
"AND" => [
"OR" => [
"worldUuid[!]" => [$worldUuid],
"worldUuid" => NULL
],
"access" => 2
],
"GROUP" => "uid"
]);
我想要 select 用户的 $worldUuid 世界。
这将生成以下 sql 语句:
SELECT "uid","displayname","surname","email" FROM "User"
LEFT JOIN "UserInWorld" ON "User"."uid" = "UserInWorld"."userUid"
WHERE ("worldUuid" NOT IN ('1') OR "worldUuid" IS NULL)
AND "access" = 2
GROUP BY "uid"
这将 select 所有(唯一)用户,他们还没有世界,或者在我正在获取用户的世界中,并且他们具有访问级别 2