mysql显示商店中最便宜的产品价格

时间:2019-01-17 21:33:50

标签: mysql sql database-design

我有桌子支票,我要在其中插入支票。 有些来自同一家商店,产品和价格重复。

所以我想查询哪个商店的价格最便宜。

Mysql表:

    +----------+---------------------+-----------+-----------------+-----------+
    |    ID    |    Product_Name     |   price   |  discount_price |  store_id |
    +----------+---------------------+-----------+-----------------+-----------+
    |    1     |   Banana 200g       |    1.25   |       0         |     1     | 
    |    2     |    Red bull 0.5l    |    2.20   |      1.90       |     1     | 
    |    3     |    Red bull 0.5l    |    2.20   |      1.90       |     1     | 
    |    4     |    Banana 300g      |    1.50   |       0         |     2     |
    |    5     |    Red bull 0.5l    |    2      |       0         |     2     |
    +----------+---------------------+-----------+--------------+--------------+

第二个问题我如何计算或查询1kg的价格?还是如果1公斤香蕉的价格为5美元,而您购买了0.200克,那么它会通过查询计算出来,还是应该用php来完成?

1 个答案:

答案 0 :(得分:1)

我知道这不能回答问题。
它仅显示了一个表结构的示例,该结构可使您的问题(以及您的问题)更容易解决。因为你要的。

Products
+----+--------+-----------+
| id |  name  |   brand   |
+----+--------+-----------+
| 1  | banana | chicita   |
+----+--------+-----------+
| 2  | banana | fairtrade |
+----+--------+-----------+

Prices
+---------+-------+----------+------+-------+
| product | store | quantity | unit | price |
+---------+-------+----------+------+-------+
| 1       | 2     | 200      | g    | 0,60  |
+---------+-------+----------+------+-------+
| 1       | 56    | 1        | kg   | 1,20  |
+---------+-------+----------+------+-------+
| 2       | 56    | 0,5      | kg   | 1,00  |
+---------+-------+----------+------+-------+

Units (should have an id, too. Made for readability here)
+------+-------------+----------+--------+-----------+
| unit |    factor   | unitgroup| locale | isDefault |
+------+-------------+----------+--------+-----------+
| g    | 1           | weight   | europe | true      |
+------+-------------+----------+--------+-----------+
| kg   | 0,01        | weight   | europe | false     |
+------+-------------+----------+--------+-----------+
| ml   | 0,001       | volume   | europe | false     |
+------+-------------+----------+--------+-----------+
| dl   | 0,1         | volume   | europe | false     |
+------+-------------+----------+--------+-----------+
| l    | 1           | volume   | europe | true      |
+------+-------------+----------+--------+-----------+
| m    | 1           | distance | europe | true      |
+------+-------------+----------+--------+-----------+
| km   | 0,001       | distance | europe | false     |
+------+-------------+----------+--------+-----------+
| mile | 0,000621371 | distance | us     | false     |
+------+-------------+----------+--------+-----------+

具有类似的结构,您可以在一个带有一些左联接,一个MIN()及其内部计算的sql查询中找到最低价格。

相关问题