简体   繁体   English

MySQL多对多查询

[英]MySQL Many-to-Many Query

I am currently in the process of converting the player saving features of a game's multi-player engine into an SQL database for the integration of a webpage to display/modify/sell characters. 我目前正在将游戏的多玩家引擎的玩家保存功能转换为SQL数据库,以集成网页以显示/修改/出售角色。 The original system stored all data into text files which was an awful way of dealing with this data as it was fixed to the game only. 原始系统将所有数据存储到文本文件中,这是处理该数据的一种糟糕方法,因为它仅适用于游戏。 Within the Text files the user's Username, Password, ID, and player-data was stored, allowing for only one character. 在文本文件中,存储了用户的用户名,密码,ID和播放器数据,仅允许一个字符。 I have separated this into tables and can successfully save and load character data. 我将其分为表格,可以成功保存和加载字符数据。 The tables I have are quite large so for example purposes I will use the following: 我的表很大,因此出于示例目的,我将使用以下表格:

account: 帐户:

+----+----------+----------+
| ID | Username | Password |
+----+----------+----------+
|  1 | Player1  | 123456   | (Secure passwords much?)
|  2 | Player2  | password | (These are actually hashed in the real db)
+----+----------+----------+

account_character: account_character:

+------------+--------------+
| Account_ID | Character_ID |
+------------+--------------+
|      1     |       1      |
|      1     |       2      |
|      2     |       3      |
+------------+--------------+

character: 字符:

+----+-----------+-----------+-----------+--------+--------+
| ID | PositionX | PositionY | PositionZ | Gender | Energy | etc....
+----+-----------+-----------+-----------+--------+--------+
|  1 |    100    |    150    |     1     |   1    |   100  |
|  2 |     30    |     90    |     0     |   1    |   100  |
|  3 |    420    |    210    |     2     |   0    |  53.5  |
+----+-----------+-----------+-----------+--------+--------+

These tables are linked using relationships. 这些表使用关系链接。

What I have so far is, the user logs in and the server queries their username and matches the password. 到目前为止,我所拥有的是,用户登录并且服务器查询其用户名并匹配密码。 If the passwords match, the server begins to load the character data based on the ID loaded from the account during logging in. 如果密码匹配,则服务器将根据登录期间从帐户加载的ID开始加载字符数据。

This is where I am stuck. 这就是我卡住的地方。 I have successfully done this through phpmyadmin using the SQL command interface, but as it was around 4AM I was tired and accidentally closed the tab that contained the command before I saved it. 我已经使用SQL命令界面通过phpmyadmin成功完成了此操作,但是由于凌晨4点左右,我很累,在保存之前不小心关闭了包含命令的选项卡。 I have tried to replicate this but I simply cannot obtain the data I require in the query. 我尝试复制此内容,但我无法获取查询中所需的数据。

I've recently completed a course in databases at college and got a distinction, but for the life of me I cannot get this to work again... I have followed tutorials but as the situations usually differ from mine I cannot apply them until I understand them. 我最近在大学完成了一门数据库课程,并获得了优异的成绩,但是对于我一生来说,我无法再继续使用它了……我遵循了教程,但是由于情况通常与我不同,因此我无法申请它们了解他们。 I know I'm going to kick myself once I have a working command. 我知道一旦有有效的命令,我就会踢自己。

Tl;dr - I wish to query all character data linked to an account using the account's 'ID'. Tl; dr-我希望使用该帐户的“ ID”查询链接到该帐户的所有字符数据。

I think this should work: 我认为这应该工作:

SELECT
    *
FROM
    account_character ac
    INNER JOIN account a ON ac.Account_ID = a.ID
    INNER JOIN character c on ac.Character_ID = c.ID
WHERE
    account.Username = ? AND
    account.Password = ?
;

We start by joining together all the relevant tables, and then filter to get characters just for the current user. 我们通过加入所有相关的表一起启动,然后筛选获得的字符都是只为当前用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM