如何在数据库的某个字段中存储信息?

发布于 2024-11-06 20:49:03 字数 519 浏览 0 评论 0原文

好吧,我目前正在为我的网站构建一个简单的购物车,并研究了这个问题,但未能成功找到答案。

到目前为止,我有这些表:

Item
------
id
categoryID
brandID
name
price



Categories
-----------
id
name



Brands
-------
id
name



Shopping Carts
--------------
id
userID
itemIDs
itemPrices
itemQuantities

根据我发现的其他答案,这似乎是正确的方法,但我不确定购物车表。我正在考虑将信息存储在这个表中,如下所示:

id: 1
userID: 1
itemIDs: 1;2;3;5
itemPrices: 40;50;60;70
itemQuantities: 1;2;3;4

这是解决这个问题的正确方法还是有更好的方法? (我还考虑在检索数据时使用 PHP 中的 Explore() 函数来分离数据。)

Ok so I am currently building a simple shopping cart for my website and have researched this question, but have been unsuccessful at finding an answer.

I have these tables so far:

Item
------
id
categoryID
brandID
name
price



Categories
-----------
id
name



Brands
-------
id
name



Shopping Carts
--------------
id
userID
itemIDs
itemPrices
itemQuantities

Based on the other answers I have found this seems to be the correct way, except I am not sure about the Shopping Carts table. I was considering storing the information in this table like so:

id: 1
userID: 1
itemIDs: 1;2;3;5
itemPrices: 40;50;60;70
itemQuantities: 1;2;3;4

Is this the correct way of tackling this or is there a better way? (I was also thinking of using the explode() function in PHP for separating the data when I will be retrieving it.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一指流沙 2024-11-13 20:49:03

理想情况下,您希望将购物车信息存储在 cookie 或会话中。如果您想将每个购物车存储在数据库中,您可以使用以下实现。

本质上,一个购物车可以有多个商品,每个商品都有与其关联的价格和数量。

要存储每个用户的购物车,请使用以下架构:

购物车

cartID(主键)
sessionID(标识创建购物车的用户会话的唯一 ID)
购买(布尔值 TRUE/FALSE,用于存储购物车是购买还是丢弃)

购物车商品

id(主键)
cartID(标识用户购物车的唯一 ID)
userID:(仅当您知道用户在将商品添加到购物车时始终处于登录状态时才使用)
itemID:购物车中商品的唯一标识符。
itemQuantity:订购商品的数量。
itemPrice:(可选 - 仅当商品价格可自定义时使用,或从商品表中提取)

使用上述架构,如果用户向您的购物车添加 2 件商品,假设

  • 商品 X(数量= 2 件,itemID = 1001) &
  • 商品 Y(数量 = 1 件,商品 ID = 1002)。

您在表中的记录如下所示:

id:1
会话ID:abc1
项目ID:1001
itemQuantity:2

id:2
会话ID:abc1
项目ID:1002
itemQuantity:1

现在,当您想要获取购物车的订单时,只需根据用户创建购物车时使用的 sessionID 进行查询即可。

Ideally, you want to store shopping cart info either in cookies or in sessions. If you want to store each shopping cart in your DB, you could use the following implementation.

Essentially, one shopping cart can have multiple items, with each item having a price and quantity associated with it.

For storing carts per user, use this schema:

Shopping Carts

cartID (primary key)
sessionID (unique ID identifying the user session in which the cart was created)
purchased (Boolean TRUE/FALSE to store whether cart was purchased or discarded)

Shopping Cart Items

id (primary key)
cartID (unique ID identifying the user cart)
userID: (use only if you know that the user will always be logged in while adding items to the cart)
itemID: Unique identifier for an item in the cart.
itemQuantity: Quantity of the item ordered.
itemPrice: (optional - use only if the item price is customizable, or pull it from the item table)

With the above schema, if a user adds 2 items to your cart, lets say

  • item X (quantity= 2pcs, itemID = 1001) &
  • item Y(quantity= 1pc, itemID = 1002).

Your records in the table would look like:

id: 1
sessionID: abc1
itemID: 1001
itemQuantity: 2

id: 2
sessionID: abc1
itemID: 1002
itemQuantity: 1

Now, when you want to fetch the order for a shopping cart - just query based on the sessionID that was in use when the user was creating the cart.

梦言归人 2024-11-13 20:49:03

不,这通常不是一个好主意。您在这里所做的是一个字段中的多个值,如果您想要查询该字段,那么这并没有什么好处。

为了满足您的迫切需要,请添加一个如下所示的新表:

CartContent
-----------
cartId
itemId
itemCount

另请参阅 http://en.wikipedia.org/ wiki/Database_normalization 是有关数据库规范化的良好入门知识。

No, that is normally not a good idea. What you are doing here is multiple values in one field and that is no good in case you ever want to query that field.

And for your pressing need, add a new table like this:

CartContent
-----------
cartId
itemId
itemCount

Also see http://en.wikipedia.org/wiki/Database_normalization for a good primer on database normalization.

罪歌 2024-11-13 20:49:03

您不应在列中使用分号分隔值,因为在执行数据库查询时无法将它们连接到各自的外部表。最好将它们存储为单独的行项目,并有一个标题表来聚合这些行项目/

You shouldn't use semicolon separated values in columns because you wont be able to join them to their respective foreign tables while doing DB queries. It is better to store them as separate line items and have a header table that aggregates these line items/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文