如何从 MySQL 表中获取最新的可用数据集(数据集每天存储)?
我有一个具有以下设置的 MySQL 表:
CREATE TABLE IF NOT EXISTS `coords` (
`project_id` int(4) NOT NULL,
`day` tinyint(4) NOT NULL,
`x` tinyint(4) NOT NULL,
`y` tinyint(4) NOT NULL,
`z` tinyint(4) NOT NULL,
PRIMARY KEY (`tid`,`day`,`x`,`y`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
每个项目都运行几天。每天都会测试一些案例,并将结果(坐标集)保存在此表中(以及项目 ID 和天数)。同一组 x 和 y 坐标可以多次保存(z 坐标是实际结果)。
现在我想获取单个项目的所有可用坐标集以及可用的最新数据。
即,假设我有以下数据:
pid | day | x | y | z
1 | 1 | 2 | 1 | 5
1 | 1 | 2 | 2 | 6
1 | 2 | 3 | 3 | 7
1 | 3 | 2 | 1 | 8
1 | 3 | 3 | 3 | 9
查询现在应返回:
day | x | y | z
1 | 2 | 2 | 6
3 | 2 | 1 | 8
3 | 3 | 3 | 9
我正在尝试以下查询:
SELECT day, x, y, z
FROM coords
WHERE pid = 1
GROUP BY CONCAT(tid,'.',x,'.',y)
ORDER BY day DESC
但是此查询返回旧数据,就像 day = 1, x|y|z = 2|1|5 而不是 day = 3 和x|y|z = 2|1|8。
有人可以指出我在这里缺少的正确方向吗?
提前致谢, 保罗
I have a MySQL table with following setup:
CREATE TABLE IF NOT EXISTS `coords` (
`project_id` int(4) NOT NULL,
`day` tinyint(4) NOT NULL,
`x` tinyint(4) NOT NULL,
`y` tinyint(4) NOT NULL,
`z` tinyint(4) NOT NULL,
PRIMARY KEY (`tid`,`day`,`x`,`y`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Each project is running several days. Each day some cases are tested and the results (set of coordinates) are saved in this table (along with project id and day number). The same set of x and y coordinates can be saved more often than once (the z coordinate is the actual result).
Now I want to get all available sets of coordinates for a single project with the latest data available.
I.e., assuming I have following data:
pid | day | x | y | z
1 | 1 | 2 | 1 | 5
1 | 1 | 2 | 2 | 6
1 | 2 | 3 | 3 | 7
1 | 3 | 2 | 1 | 8
1 | 3 | 3 | 3 | 9
The query shall now return:
day | x | y | z
1 | 2 | 2 | 6
3 | 2 | 1 | 8
3 | 3 | 3 | 9
I was trying following query:
SELECT day, x, y, z
FROM coords
WHERE pid = 1
GROUP BY CONCAT(tid,'.',x,'.',y)
ORDER BY day DESC
But this query returns old data, just as day = 1, x|y|z = 2|1|5 instead of day = 3 and x|y|z = 2|1|8.
Could someone please point me in the right direction of what I am missing here?
Thanks in advance,
Paul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:)。
Try this out :).
好吧,我认为你在那里遇到了问题,因为一旦你执行
Select * from coords order by day asc limit 1
你就会随机得到。 2.1.5 或 2.2.6。取决于数据库的感受。您肯定需要更具体的“dayting”或 ID 或根据数据库知道这些行的插入顺序的内容。例如,在那里添加一个 id 字段。如果您在其中添加 id 字段,这可能会产生魔力
well i thing you have a problem there because once you do
Select * from coords order by day asc limit 1
you will get randomly. 2.1.5 or 2.2.6. depending on how the database feels it. You definitly need more specific "dayting" or ID or something according to what will the database know in what order those lines were inserted in. Add an id field there for example.This might do the magic if you add the id field in there