如何从 MySQL 表中获取最新的可用数据集(数据集每天存储)?

发布于 2024-10-16 06:43:40 字数 982 浏览 2 评论 0原文

我有一个具有以下设置的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独留℉清风醉 2024-10-23 06:43:40

试试这个:)。

SELECT
  day,
  x,
  y,
  z 
FROM
  coords
    JOIN
  (SELECT MAX(tid) AS max_tid FROM coords GROUP BY CONCAT_WS('.', day, x, y)) sub ON (sub.max_tid = coords.tid)
WHERE
  pid = 1

Try this out :).

SELECT
  day,
  x,
  y,
  z 
FROM
  coords
    JOIN
  (SELECT MAX(tid) AS max_tid FROM coords GROUP BY CONCAT_WS('.', day, x, y)) sub ON (sub.max_tid = coords.tid)
WHERE
  pid = 1
三生池水覆流年 2024-10-23 06:43:40

好吧,我认为你在那里遇到了问题,因为一旦你执行 Select * from coords order by day asc limit 1 你就会随机得到。 2.1.5 或 2.2.6。取决于数据库的感受。您肯定需要更具体的“dayting”或 ID 或根据数据库知道这些行的插入顺序的内容。例如,在那里添加一个 id 字段。

Select distinct day, x,y,z 
FROM coords
where pid = 1
order by id desc

如果您在其中添加 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.

Select distinct day, x,y,z 
FROM coords
where pid = 1
order by id desc

This might do the magic if you add the id field in there

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文