繁体   English   中英

订单表结构,mysql

[英]Orders table structure, mysql

只是一个简单的问题 - 如何组织订单表,即当有人订购 id=1 的 2x 项目和 id=2 的 3x 项目时。

我以前的解决方案是将它保存为: 2x1,3x2 in products列,然后explode() 它,但效率很低。

我会用 3 张桌子:

  • product表——完全独立于订购系统,网站仅使用它来显示产品
  • order表,存储order的基本信息(如谁下单、账单地址等)
  • 以及这order_product之间的order_product连接表,指示每个订单包含哪些产品以及数量。


最后一个表至少包含以下字段:

  • id_order : 订单标识符
  • id_production : 产品的标识符
  • quantity : 该产品在此订单中的购买次数

我认为接受的答案非常好,但我也会用customers表来扩展它。

这是我计划用于我的项目的结构的更完整示例(尚未测试)...

CUSTOMERS table:
    _id
    name
    address
    tel
    email

PRODUCTS table:
    _id
    name
    price

ORDERS table:
    _id
    customer_id
    datetime

ORDER_PRODUCTS table:
    _id
    order_id
    product_id
    product_quantity

...所以,基本上,如果客户的订单中有 3 种不同的产品(例如,1 个苹果、2 个香蕉和 4 顶帽子),那么我们会看到 1 行被添加到ORDERS表中,3 行被添加到ORDER_PRODUCTS桌子。

这是另一个提议,稍微扩展一点:

客户表:

id
name 
email
timestamps  (created, modified, deleted)

客户地址:

id
customer_id
street
zip_code
city
state
country
type (enum or varchar indexed  - options would be 'billing', 'shipping')

订单:

id
customer_id
subtotal (without taxes, discount, shipping, etc.)
total (including additional order line items)
billing_address_id (foreign key from customer_addresses) 
shipping_address_id (foreign key from customer_addresses) 
status (paid, checkout, canceled, failed, expired...)
payment type
timestamps (created, modified, deleted)

订单项

id
order_id
item_id
item_quantity
price 

order_line_items(可选,用于存储额外费用,如运费、折扣、税费等):

id
order_id
type
amount
timestamps

暂无
暂无

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

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