简体   繁体   English

如何选择在给定日期尚未更新的MySQL表值?

[英]How do I SELECT a MySQL Table value that has not been updated on a given date?

I have a MySQL database named mydb in which I store daily share prices for 423 companies in a table named data. 我有一个名为mydb的MySQL数据库,其中将423家公司的每日股价存储在名为data的表中。 Table data has the following columns: 表数据包含以下列:

`epic`, `date`, `open`, `high`, `low`, `close`, `volume`

epic and date being primary key pairs. epicdate是主键对。 I update the data table each day using a csv file which would normally have 423 rows of data all having the same date. 我每天都使用一个csv文件更新数据表,该文件通常会包含423行数据,且所有数据都具有相同的日期。 However, on some days prices may not available for all 423 companies and data for a particular epic and date pair will not be updated. 但是,有时可能无法为所有423家公司提供价格,并且特定史诗和日期对的数据也不会更新。 In order to determine the missing pair I have resorted to comparing a full list of epics against the incomplete list of epics using two simple SELECT queries with different dates and then using a file comparator, thus revealing the missing epic(s). 为了确定丢失的对,我已采取措施,使用两个具有不同日期的简单SELECT查询,然后使用文件比较器,将完整的史诗列表与不完整的史诗列表进行比较,从而揭示丢失的史诗。 This is not a very satisfactory solution and so far I have not been able to construct a query that would identify any epics that have not been updated for any particular day. 这不是一个非常令人满意的解决方案,到目前为止,我还无法构造一个查询来标识在任何特定日期尚未更新的史诗。

SELECT `epic`, `date` FROM `data`
WHERE `date` IN ('2019-05-07', '2019-05-08')
ORDER BY `epic`, `date`;

Produces pairs of values: 产生一对值:

`epic`  `date`
"3IN"   "2019-05-07"
"3IN"   "2019-05-08"
"888"   "2019-05-07"
"888"   "2019-05-08"
"AA."   "2019-05-07"
"AAL"   "2019-05-07"
"AAL"   "2019-05-08"

Where in this case AA. 在这种情况下,AA。 has not been updated on 2019-05-08. 尚未于2019-05-08更新。 The problem with this is that it is not easy to spot a value that is not a pair. 问题是发现不成对的值并不容易。 Any help with this problem would be greatly appreciated. 任何有关此问题的帮助将不胜感激。

You could do a COUNT on epic, with a GROUP BY epic for items in that date range and see if you get any with a COUNT less than 2, then select from this result where UpdateCount is less than 2, forgive me if the syntax on the column names is not correct, I work in SQL Server, but the logic for the query should still work for you. 您可以对该日期范围内的项目执行史诗般的COUNT操作,并使用GROUP BY史诗进行操作,然后查看是否得到COUNT小于2的任何内容,然后从此结果中选择UpdateCount小于2的代码,请原谅我列名不正确,我在SQL Server中工作,但是查询的逻辑仍然对您有用。

SELECT x.epic
FROM
(
    SELECT COUNT(*) AS UpdateCount, epic
    FROM data
    WHERE date IN ('2019-05-07', '2019-05-08')
    GROUP BY epic
) AS x
WHERE x.UpdateCount < 2

Assuming you only want to check the last date uploaded, the following will return every item not updated on 2019-05-08: 假设您只想查看上次上传的日期,则以下内容将返回2019-05-08上未更新的每个项目:

SELECT last_updated.epic, last_updated.date
FROM (
SELECT epic , max(`date`) AS date FROM `data`
GROUP BY 'epic'
) AS last_updated
WHERE 'date' <> '2019-05-08'
ORDER BY 'epic'
;

or for any upload date, the following will compare against the entire database, so you don't rely on '2019-08-07' having every epic row. 或对于任何上载日期,以下内容将与整个数据库进行比较,因此您不必依靠'2019-08-07'的每一行都是史诗级的。 Ie if the epic has been in the database before then it will show if not updated: 也就是说,如果史诗在此之前已经存在于数据库中,那么它将显示是否未更新:

SELECT d.epic, max(d.date)
FROM data as d
WHERE d.epic NOT IN (
   SELECT d2.epic 
   FROM data as d2 
   WHERE d2.date = '2019-05-08'
)
GROUP BY d.epic
ORDER BY d.epic

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

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