douduo2407 2013-12-05 12:31
浏览 13
已采纳

需要从DB中获取ID未知的所有记录

in my project, the user can enter categories into a budget. So the user enters an annual amount in a form field. They can then select "monthly" or "annual" from a dropdown menu, and also select which month they want the annual category to start on. So if they select annual and June, then records would be stored into the categories table, one for each month (June, July, August, etc.).

Here's my problem. I want to be able to get a running sum total of the amounts in a particular category to display to the user on the page, but when an annual category is stored to the database, its given a unique id as the primary key, and that's obsiously auto-incremented.

So where I would normally select records where the ID = x, I can't do that because the ID is not the same.

The only way around it that I can think of is to get them by category name since they would all be the same. Is that even ok to do? There's nothing to stop the user from going in and creating another category with the exact same name, since the name is not unique.

  • 写回答

1条回答 默认 最新

  • dongyi6845 2013-12-05 12:37
    关注

    You've already answered your question. If the user creates two categories with the same name the page will appear broken.

    Try to normalize your schema, it will probably help you.

    Something along the lines of

    budget_entry_per_year(category, amount, starting_month)
    budget_entry_per_month(category, amount)

    You can add IDs that are unique to both tables if needed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程