简体   繁体   English

我要加一个还是算一个以从DB获得预订号?

[英]Shall I add one or count to get booked number from DB?

I have the following db for a fitness club calendar and booking system. 我有一个健身俱乐部日历和预订系统的以下数据库。 I get omc_courses details which joined to omc_date,omc_week etc. 我获得了omc_courses的详细信息,这些详细信息已加入omc_date,omc_week等。

Now I also want to display total booked numbers for each course. 现在,我还想显示每个课程的总预订人数。

I thought two ways to do it and I'd like to know your opinions which way is better or if there are any better ways to do it. 我想了两种方法来做,我想知道您的意见,哪种方法更好,或者是否有更好的方法。

My method one. 我的方法一。

Adding and deleting one to/from omc_courses.booked whenever a member booked or cancel the booking. 每当成员预订或取消预订时,就在omc_courses.booked中添加一个或从其中删除一个。

My method two. 我的方法二。

Count omc_bookings by course_id, grouped_by course_id and add this number to omc_courses.booked. 按课程编号对omc_bookings进行计数,按课程编号对grouped_by进行计数,然后将此数字添加到omc_courses.booked中。

Thanks in advance. 提前致谢。

CREATE TABLE IF NOT EXISTS `omc_bookings` (
  `booking_id` int(11) NOT NULL AUTO_INCREMENT,
  `customer_id` int(11) NOT NULL,
  `course_id` int(11) NOT NULL,
  `date_enroll` date NOT NULL,
  PRIMARY KEY (`booking_id`)
) .... ;

    CREATE TABLE IF NOT EXISTS `omc_courses` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `date_id` int(10) DEFAULT NULL,
  `time` time NOT NULL,
  `course_name` varchar(255) DEFAULT NULL,
  `trainer_id` int(11) DEFAULT NULL,
  `desc` varchar(255) DEFAULT NULL,
  `capacity` int(11) DEFAULT NULL,
  `active` tinyint(1) NOT NULL DEFAULT '1',
  `order` int(11) DEFAULT NULL,
  `booked` int(5) DEFAULT NULL,
  `type` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ..... ;

  there are more like omc_date, omc_week etc.

I'd do both, because it allows one to act as a verification for the other -- if the two don't match then you've got a bug somewhere. 我会两者都做,因为它允许一个人充当另一个人的验证-如果两者不匹配,那么您在某个地方有一个错误。 But if you've only got one you may never even spot the bug. 但是,如果只有一个,那么您甚至可能根本找不到该错误。

i would use count - why store a value that can be calculated so easy? 我会使用count-为什么存储可以轻松计算的值? (and with the stored value you're exposed to danger to forget to increase/decrease you number anywhere, so that the stored number isn't correct at all) (由于储值,您很容易忘记在任何地方增加/减少您的号码,因此所存储的号码根本不正确)

I would not store data in the database that I can get with a query unless in soem very specific cases maybe. 除非在非常特殊的情况下,否则我不会将数据存储在可以通过查询获得的数据库中。 In your case you do not need to store booked number anywhere. 就您而言,您无需在任何地方存储预订号。 Just run the COUNT query when you need it. 只需在需要时运行COUNT查询。

If you can get the information in a query, then don't bother adding it as a value in another table, this takes your database structure from being a true relational database. 如果您可以在查询中获取信息,那么就不必将其作为值添加到另一个表中,这会使您的数据库结构成为真正的关系数据库。

Your current set-up is fine, where you have a table for courses and a table for bookings. 您当前的设置很好,那里有课程表和预订表。 So just do a COUNT query using the course ID and time in your WHERE clause to filter. 因此,只需使用WHERE子句中的课程ID和时间进行COUNT查询即可进行过滤。

自己回答:您是否需要数据库永久地,每秒运行一次的信息(然后将其存储在数据库中)或仅在一种(或两种)特定情况下需要它(然后对您的数据进行计数)结果)?

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

相关问题 从数据库获取最大值并添加一个 - Get Max value from db and add one MySQL-如何获得连续预定天数的用户? - Mysql - How to get users who booked a certain number of days in a row? 添加从数据库获得的数字 - Add the number gotten from a db 我对codeIgniter的教程数量不知所措。 我应该从cakePHP切换到它吗? - I'm overwhelmed by the number of tutorials for codeIgniter. Shall I switch to it from cakePHP? Codeigniter-从一个数据库表中获取价值,并在表单提交时添加到其他数据库表中 - Codeigniter - Get value from one DB Table and Add to other DB table on form submit 如何计算一列中的出现次数并将下一列添加到 MYSQL、PHP 并呈现给数据表 - How can I count number of occurrence in one column and add the next respective column in MYSQL, PHP and present to datatable 如何自动计算数字和每行自动添加逗号(从我想要的数字开始) - How to auto count number and auto add comma per rows ( start from number i want ) 计算已从数据库中选择每个实例的次数 - Count The Number Of Times Each Instance has been selected from DB 自定义MVC:从数据库中选择计数并显示行数 - Custom MVC: select count from db and display number of rows 对于一个表的每个值,从另一个表中获取并显示相应值的计数 - for each value of one table get and display count number of coresponding values from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM