php - 与货币和国家相关的产品的数据库结构

标签 php mysql database laravel

在我的申请中,有很多公司提供的产品/服务。

这是结构:

cities:

id
city_name
status
country

products:

id
product_name
product_price
status
metadata

countries:

id
country_name
currency
status

currencies

id
currency
symbol
code

目前,

  1. 一个国家有很多城市
  2. 一个国家有一种货币

现在,每个产品在不同的城市会有不同的价格。

如何根据城市映射产品价格? 什么是通用结构,它可以满足所有用例?

最佳答案

使用规范化有帮助。

cities table:
id
name
status
country_id (id in table countries)

countries table:
id
name
currency_id (id in table currencies)
status

products table:
id
name
**    product_price ** (should be removed from this table)
status
metadata 

currencies table:
id
currency
symbol
code

product_price_in_city table:
id
city_id     (id in table cities)
product_id  (id in table products)
product_price_in_city (just a number. currency and symbol should be retrieved from table currency. using JOINs.
**'city_id ,product_id' pair should be unique.**

cities:
ID | name       | country_id
1  | 'london'   | 98
2  | 'tokyo'    | 99

countries:
ID | name       | currency_id
98 | 'england'  | 121
99 | 'japan'    | 122

currencies:
ID | name       | symbol
121| 'Pound'    | 's1'
122| 'Yen'      | 's2'

products:
ID | name 
789| 'CD'
790| 'DVD'
791| 'Floppy !!!!'

product_price_in_city:
ID  | city_id  | product_id | product_price_in_city
9988| 1        | 789        | 667788
9989| 1        | 790        | 66779988
9990| 1        | 791        | 567
9991| 2        | 790        | 776655
9992| 2        | 791        | 998877

如你所见,

CD  in London is 667788   Pound
DVD in London is 66779988 Pound
DVD in Tokyo  is 776655   Yen

关于php - 与货币和国家相关的产品的数据库结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34103233/

相关文章:

database - 一对多数据库关系

mysql - 如何使用外键重新排序/重置自动增量主键?

php - 如何为数组分配秩数

php - WooCommerce 检查订单是否已支付/处理或完成

php - 匹配 HH :MM:SS time format in php? 的正则表达式

php - 通过 javascript/jquery 运行 php sql 查询来隐藏条目

php - 为什么在 MySQL 中使用 SUM() 时会得到重复的值?

mysql - ALTER TABLE 添加复合主键

php - 创建一个 mysql 触发器以在列更新时插入数据

php - 谁有好的 PHP 脚本可以作为 cronjob 运行来备份 MySQL 数据库?