简体   繁体   English

MySQL 数据库中的最小值

[英]Min value from Database in MySQL

Am trying to find the min value from past 30 days, in my table there is one entry for every day, am using this query我试图找到过去 30 天的最小值,在我的表中每天有一个条目,我正在使用这个查询

SELECT MIN(low), date, low 
FROM historical_data 
WHERE name = 'bitcoin' 
ORDER BY STR_TO_DATE(date,'%d-%m-%Y') DESC 
LIMIT 7

But this value not returing the correct value.但是这个值没有返回正确的值。 The structure of my table is我的表的结构是

Table structure表结构在此处输入图片说明

And table data which is store is like this存储的表数据是这样的

Table data style表格数据样式在此处输入图片说明

Now what i need is to get the minimum low value.现在我需要的是获得最小的低值。 But my query not working it give me wrong value which even did not exist in table as well.但是我的查询不起作用它给了我错误的值,甚至在表中也不存在。

Updates:更新:

Here is my updated Table Structure.这是我更新的表结构。 enter image description here在此处输入图片说明

And here is my data in this table which look like this enter image description here这是我在这张表中的数据,看起来像这样在此处输入图像描述

Now if you look at the data, i want to check the name of token omisego and fatch the low value from past 7 days which will be from 2017-12-25 to 2017-12-19 and in this cast the low value is 9.67 , but my current query and the query suggested by my some member did not brings the right answer.现在,如果你看一下数据,我想检查令牌的名称omisego和混帐从过去7天的低价值,这将是自2017-12-252017-12-19 ,并在这个转换的低值9.67 ,但我当前的查询和我的某个成员建议的查询没有带来正确的答案。

Update 2:更新 2:

http://rextester.com/TDBSV28042

Here it is, basically i have more then 1400 coins and token historical data, which means that there will me more then 1400 entries for same date like 2017-12-25 but having different name, total i have more then 650000 records.在这里,基本上我有超过1400 coinstoken历史数据,这意味着我将有超过 1400 个条目与2017-12-25相同的日期但名称不同,我总共有超过650000条记录。 so every date have many entries with different names.所以每个日期都有许多不同名称的条目。

To get the lowest row per group you could use following要获得每组最低的行,您可以使用以下内容

SELECT a.*
FROM historical_data a 
LEFT JOIN historical_data b ON a.name = b.name 
AND a.low > b.low
WHERE b.name IS NULL
AND DATE(a.date) >= '2017-12-19' AND DATE(a.date) <= '2017-12-25' 
AND a.name = 'omisego'

or或者

SELECT a.*
FROM historical_data a 
JOIN (
    SELECT name,MIN(low) low
    FROM historical_data
    GROUP BY name
) b USING(name,low)
WHERE  DATE(a.date) >= '2017-12-19' AND DATE(a.date) <= '2017-12-25' 
AND a.name = 'omisego'

DEMO演示

For last 30 day of 7 days or n days you could write above query as对于 7 天或 n 天的最后 30 天,您可以将上述查询写为

SELECT a.*, DATE(a.`date`)
FROM historical_data2 a 
LEFT JOIN historical_data2 b ON a.name = b.name 
AND DATE(b.`date`) >= CURRENT_DATE() - INTERVAL 30 DAY 
AND DATE(b.`date`) <= CURRENT_DATE()
AND a.low > b.low
WHERE b.name IS NULL
AND a.name = 'omisego'
AND DATE(a.`date`) >= CURRENT_DATE() - INTERVAL 30 DAY
AND DATE(a.`date`) <= CURRENT_DATE()
;

DEMO演示

But note it may return more than one records where low value is same, to choose 1 row among these you have specify another criteria to on different attribute但请注意,它可能会返回多个低值相同的记录,要在其中选择 1 行,您必须在不同的属性上指定另一个条件

Consider grouping the same and running the clauses考虑将相同的分组并运行子句

SELECT name, date, MIN(low)
FROM historical_data 
GROUP BY name
HAVING name = 'bitcoin' 
AND STR_TO_DATE(date, '%M %d,%Y') > DATE_SUB(NOW(), INTERVAL 30 DAY);

Given the structure, the above query should get you your results.鉴于结构,上面的查询应该会得到你的结果。

// Try this code .. // 试试这个代码 ..

SELECT MIN(`date`) AS date1,low
        FROM historical_data
 WHERE `date` BETWEEN now() - interval 1 month 
                  AND now() ORDER by low ASC;

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

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