Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mysql显示商店中最便宜的产品价格_Mysql_Sql_Database Design - Fatal编程技术网

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

mysql显示商店中最便宜的产品价格,mysql,sql,database-design,Mysql,Sql,Database Design,我有桌上支票,我在那里插入支票。 有些来自同一家商店,产品和价格重复 所以我想查询一下哪个商店的产品价格最便宜 我的Mysql表: +----------+---------------------+-----------+-----------------+-----------+ | ID | Product_Name | price | discount_price | store_id | +----------+------

我有桌上支票,我在那里插入支票。 有些来自同一家商店,产品和价格重复

所以我想查询一下哪个商店的产品价格最便宜

我的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克,那么它会用query计算,或者应该用php计算?

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

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     |
+------+-------------+----------+--------+-----------+

对于这样的结构,您可以在一个sql查询中找到最低价格,其中包含一些左连接、一个MIN()和一些计算。

可能的重复会很复杂,因为这些值没有分开……您不能更改数据放入数据库的方式吗?(把“香蕉”和“200g”分开)@Jeff那我该怎么把桌子分开呢?我用支票把产品存入数据库,我有杂货店的支票,我把所有的产品和价格都存入数据库。。但是我必须创建不同的表?PHP代码在哪里?我不知道怎么做,怎么查询所有的数据,或者怎么用php实现这些@Dharmand注意到group是一个保留字,因此对于表/列来说是一个糟糕的选择identifier@Strawberry没错,我会改变的@Jeff为什么要使用列“isDefualt”我能做什么或我必须做什么?有了列“isDefault”,你的应用程序就会知道它应该显示为标准的单位,你就会知道你必须计算价格的单位。只是一个建议。你们也可以在产品表中有一个“单位”栏,这样香蕉以g表示,梅隆以kg表示。所有这些都只是输出的预设值。