gpt4 book ai didi

mysql - 查询SQL限制

转载 作者:行者123 更新时间:2023-11-29 07:23:13 25 4
gpt4 key购买 nike

表格

玩家

id | name    | rank id
1 | player1 | 3
2 | player2 | 0
3 | player3 | 0
4 | player4 | 1
5 | player5 | 0
6 | player6 | 0

公会

id | name
1 | test1
2 | test2

公会等级

id | guild_id
1 | 1 <- which is test
2 | 2 <- which is test2

查询

$player = query -> name
$player_id = $SQL->query('SELECT rank_id from players where name = "'.htmlspecialchars($player['name']).'"')->fetch();
$player_id = $player_id['rank_id'];
$has_guild = $SQL->query('SELECT guild_id from guild_ranks where id = '. $player_id .'')->fetch();
$has_guild = $has_guild['guild_id'];
if($has_guild == 0){
$guild_d_player = "No Guild";
}else{
$guild_do_player = $SQL->query('SELECT name from guilds where id = '.$has_guild.' > 0')->fetch();
$guild_do_player = $guild_do_player ['name'];
$guild_d_player = "";
}

这就是发生的情况:

这是关于公会的。

示例:

Name    |   Guild Name   | This player truly is in a guild?

Player1 | Test | Yes

Player2 | TestNo Guild | <- Here is the problem, this player doesn't have guild but seems like if he was on one.

Player3 | TestNo Guild | No

Player4 | Test2 | Yes

Player5 | Test2No Guild | No

Player6 | Test2No Guild | No

Etc...

它会重复公会名称,直到查询得到一个真正存在的新公会名称。

完整查询:http://pastebin.com/03TYv9V9

最佳答案

您应该使用 left joins 的单个查询连接 3 个表:

SELECT g.name from players p
left join guild_ranks gr on p.rank_id=gr.id
left join guilds g on g.id=gr.guild_id
where name=...

如果返回值为空(null),则该玩家没有公会。

进一步说明:

  1. 您不应通过玩家查询此信息。您应该将此查询与pastebin 中代码顶部的主要查询结合起来。只需更改其中使用的逗号语法即可显式连接。

  2. 您提供的示例数据与预期结果不一致。 Player1的rank_id为3,但guild_rank表中没有id=3的记录,但从生态结果来看,该玩家属于test1公会。

  3. 我不明白 guild_rank 表的用途,也不明白为什么您将玩家链接到该表,而不是将 guild_id 存储在玩家表中。

关于mysql - 查询SQL限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35375326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com