如何对mysql中的数据进行编号

发布于 2024-08-26 23:20:43 字数 392 浏览 5 评论 0原文

这是对mysql select查询的疑问 让我用一个简单的例子来解释我的疑问 认为这是我的查询 SELECT dbCountry from tableCountry

tableCountry 具有字段 dbCuntryId、dbCountry 和 dbState 我得到了结果,因为

dbCountry 
india 
america 
england
kenya 
pakisthan

我需要结果,因为

1  india 
2  america 
3  england
4  kenya 
5  pakisthan

数字 12345 必须随着数据的增加而生成,并且它不是自动增量 id。 我怎样才能得到它 是不是类似循环的东西

This is a doubt on mysql select query
let me axplain my doubt with a simple example
consider this is my query
SELECT dbCountry from tableCountry

tableCountry has fields dbCuntryId, dbCountry and dbState
I have the result as

dbCountry 
india 
america 
england
kenya 
pakisthan

I need the result as

1  india 
2  america 
3  england
4  kenya 
5  pakisthan

the numbers 12345 must be generated with the increase in data and it is not an autoincrement id.
How can i get it
is it something like loop

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

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

发布评论

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

评论(2

雨的味道风的声音 2024-09-02 23:20:43

你可以试试这个:

SELECT dbCountry,
(SELECT COUNT(*) FROM tableCountry t2 WHERE t2.dbCountry <= t1.dbCountry) 
AS RowNum
FROM tableCountry t1
ORDER BY dbCountry

You can try this:

SELECT dbCountry,
(SELECT COUNT(*) FROM tableCountry t2 WHERE t2.dbCountry <= t1.dbCountry) 
AS RowNum
FROM tableCountry t1
ORDER BY dbCountry
因为看清所以看轻 2024-09-02 23:20:43

以下应该可以满足您的需要。它使用一个为每行递增并返回的变量:

SELECT
  @rownum:=@rownum+1 number,
  c.dbCountry
FROM
  tableCountry c,
  (SELECT @rownum:=0) r

如果您希望结果始终保持相同的顺序,则需要向查询添加 order by 约束,例如 ORDER BY c.dbCountry 按国家/地区名称排序。

The following should do what you need. It uses a variable that is incremented and returned for each row:

SELECT
  @rownum:=@rownum+1 number,
  c.dbCountry
FROM
  tableCountry c,
  (SELECT @rownum:=0) r

If you want the result to always be in the same order you'll need to add an order by constraint to the query, for example, ORDER BY c.dbCountry to order by the country name.

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