数据库数据中拥有自己的计数器

发布于 2024-12-12 00:21:19 字数 856 浏览 0 评论 0原文

id | num | text
1  | a   | sdfsdf
2  | a   | sdfsdf
3  | b   | sdfsdf
4  | c   | sdfsdf
5  | a   | sdfsdf
6  | b   | sdfsdf
7  | a   | sdfsdf
8  | c   | sdfsdf

我在 num = a 和 orderBy ID DESC 处进行查询,我有:

id | num | text
7  | a   | sdfsdf
5  | a   | sdfsdf
2  | a   | sdfsdf
1  | a   | sdfsdf

我想为这个自己的计数器添加:

count | id | num | text
  4   | 7  | a   | sdfsdf
  3   | 5  | a   | sdfsdf
  2   | 2  | a   | sdfsdf
  1   | 1  | a   | sdfsdf

我使用 foreach PHP 获取数据:

foreach ($data as $i => $dat) {
   echo $dat->id . $dat->num . $dat->text;
}

我想要一些东西:

foreach ($data as $i => $dat) {
   echo $i . $dat->id . $dat->num . $dat->text;
}

但 $i 方向相反

id | num | text
1  | a   | sdfsdf
2  | a   | sdfsdf
3  | b   | sdfsdf
4  | c   | sdfsdf
5  | a   | sdfsdf
6  | b   | sdfsdf
7  | a   | sdfsdf
8  | c   | sdfsdf

i make query where num = a and orderBy ID DESC and i have:

id | num | text
7  | a   | sdfsdf
5  | a   | sdfsdf
2  | a   | sdfsdf
1  | a   | sdfsdf

i would like add for this own counter:

count | id | num | text
  4   | 7  | a   | sdfsdf
  3   | 5  | a   | sdfsdf
  2   | 2  | a   | sdfsdf
  1   | 1  | a   | sdfsdf

i get data with foreach PHP:

foreach ($data as $i => $dat) {
   echo $dat->id . $dat->num . $dat->text;
}

i would like somethings:

foreach ($data as $i => $dat) {
   echo $i . $dat->id . $dat->num . $dat->text;
}

but $i in the opposite direction

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

捂风挽笑 2024-12-19 00:21:19

你可以这样做:

$count = count($data);
foreach ($data as $i => $dat) {
   echo ($count - $i) . $dat->id . $dat->num . $dat->text;
}

You can do this:

$count = count($data);
foreach ($data as $i => $dat) {
   echo ($count - $i) . $dat->id . $dat->num . $dat->text;
}
绝對不後悔。 2024-12-19 00:21:19

在 PHP 中:

$i = count($data);
foreach ($data as $dat) {
    echo $i . $dat->id . $dat->num . $dat->text;
    $i--;
}

In PHP:

$i = count($data);
foreach ($data as $dat) {
    echo $i . $dat->id . $dat->num . $dat->text;
    $i--;
}
童话里做英雄 2024-12-19 00:21:19

您知道,您可以在 SQL 查询中解决这个问题。这是针对生产 Redmine 数据库实例 (MySQL) 运行的示例:

select @curRow := @curRow + 1 AS row_number, issues.*
from issues, (SELECT @curRow := 0) r
where project_id = 7
and tracker_id = 3
order by 1 desc

You know, you can solve that in your SQL query. This is a sample which a ran against a production Redmine DB instance (MySQL):

select @curRow := @curRow + 1 AS row_number, issues.*
from issues, (SELECT @curRow := 0) r
where project_id = 7
and tracker_id = 3
order by 1 desc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文