有没有更有效的方法呢?
我最近开始使用 PHP 和 mysql 数据库创建一个“游戏”。 在这个游戏中,有船只,每个玩家都可以建造和控制。
现在我的数据库中有几个这样的设置:
船舶表,包含这些列:
ID、名称、积分成本、矿物成本、建造时间、描述、速度、功率、护盾
用户表包含:
Ship_01_amount .... Ship_08_amount
其中数字对应于船舶的 ID。
我想知道是否有一种更有效的方法来做到这一点,同时又不会把事情搞砸。
从现在开始,如果我想添加一艘新船,我就必须更改我的表。
I recently started on creating a 'game' using PHP and a mysql database.
In this game, there are ships, which each player can build and control.
Right now I have several this set up in my database like this:
Ships table, contains there columns:
ID, name, credits cost, mineral cost, building time, description, speed, power, shield
The users table holds:
Ship_01_amount .... ship_08_amount
Where the number corresponds to the id of the ship.
I was wondering if there is a more efficient way of doing this, while not screwing things up.
Since now, if I wish to add a new ship, I have to alter my table.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您必须将有关用户及其船只的信息拆分到第三个表中。因此,您将拥有以下表格:
这样,当添加船舶时,您只需更新表 t_user_ships 中的金额。
You have to split the information about users and their ships into a third table. So you would have following tables:
This way when adding a ship you would just update the amount in the table t_user_ships.
阅读数据库规范化会有很大帮助。一个好的资源如下:http://dev.mysql。 com/tech-resources/articles/intro-to-normalization.html
虽然我知道这并不能直接回答您的问题,但它将有助于解决未来的此类困境。在 Google 上搜索“一对多 mysql”或“多对一 mysql”将会找到一些有用的资源。
亲切的问候
It would help a lot to read up on database normalization. A good resource is as follows: http://dev.mysql.com/tech-resources/articles/intro-to-normalization.html
While I know this is not answering your question directly, it will help with future predicaments like these. A Google search for "one to many mysql" or "many to one mysql" will pull up some useful resource.
Kind regards
搜索“什么是多对多关系”。
或检查相关问题/答案: resolve-many-to-many-relationship
全部其他人已经回答,“中间”或“中间”表是解决方案的关键。有关特定用户的特定船舶的任何信息(例如您拥有的数量)都可以添加为该表中的字段。无论您的游戏需要什么,您都可以添加旗帜颜色、当前速度、船长姓名、首次航行日期等。
Search for "what is many to many relationship".
Or check relevant question/answer: resolve-many-to-many-relationship
As all other have answered, a "middle" or "intermediate" table is the key to the solution. Any info (like the amount you have) about a particular ship of a particular user can be added as a field in that table. You could add colour-of-flag-raised, current-speed, captain's-name, date-of-first-sail, etc, whatever your game needs.
是的,您可以使用多对多关系 通过创建用户和船舶之间的新表 User_has_ship(userId, ShipId)。
您可以使用 外键约束 MYSQL 检查用户和船舶是否存在。
尽管检查用户船舶现在需要表连接,所以你必须这样做
你应该替换?在 userId 中包含您当前正在检查的用户的 ID。
Yes you can use a many-to-many relationship by creating a new table mapping between users and ships User_has_ship(userId, shipId).
You can use foreign key constraints in MYSQL to check that the user and the ship exists.
Altough check for user ships now needs a table join, so you have to do like this
You should replace ? in userId with the id for the user you are currently checking.
基本上将其分成三个表。
表 3:
Ships_User_META
...等等。
因此,您可以轻松地向用户添加和删除船舶。
Make this basically into three tables.
Table 3:
Ships_User_META
...and so on.
Therefore you can easily add and remove ships to users.
您似乎有一个包含统一船舶描述的表,以及一个将船舶引用作为列的表。这里有的是您的用户和船舶参考之间的关系。扩展它需要更改您的表架构。
您提出以下建议:
创建一个包含三列的表 user_ships :
- user_id 字段,
- Ship_id 字段,
- 金额字段
每次您需要知道属于单个玩家的船只数量时,您可以对与相应 ID 连接的用户、船只和 user_ships 表执行 SELECT。
当用户构建新的船舶模型时,在 user_ships 表中插入一条记录。
当用户丢失或建造更多船只时,更新相应的记录。
就是这样!
You seem to have a table with unitary ship descriptions, and a table owning references to ships as columns. What you have here is relation between your user and ship references. Extending this requires altering your table schema.
You advise the following :
Create a table user_ships with three columns :
- a user_id field,
- a ship_id field,
- an amount field
Every time you need to know the amount of ships belonging to a single player you can do a SELECT on users, ships and user_ships tables joined with corresponding ids.
When a user builds a new model of ship, INSERT a record in user_ships table.
When a user loses or builds more ships, UPDATE the corresponding record.
That's it!