根据特定列的数据自动递增 [英] auto increment by specific column's data

查看:45
本文介绍了根据特定列的数据自动递增的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在如下表中,


category | feeds | content
==========================
1          1       something
1          2       bar
1          3       foo
2          1       blah
2          2       um
1          4       things

我应该如何独立工作auto_increment(按类别"区分)?

How should I do to work auto_increment independently (distinguished by 'category')?

谢谢.

推荐答案

对于oracle,我们有 ROW_NUMBER()OVER PARTITION BY 语句,但是对于Mysql来说,实现此语句仍然很棘手.最有可能的是,我们将使用此实现来限制数据在组之间的降序/升序.

When it comes to oracle we have ROW_NUMBER() OVER PARTITION BY statement but for Mysql it is still tricky to implement this one. As most probably we will use this implementation to limit data either descending/ascending order across groups.

在这里,您可以使用类似的实现方式作为参考:

Here you go with the similar implementation for your reference:

SELECT @row_number:=CASE WHEN @category=category 
THEN @row_number+1 ELSE 1 END AS feeds,@category:=category AS category, content
FROM table1, (SELECT @row_number:=0,@category:='') AS t
ORDER BY category;

SQLFiddle链接

这篇关于根据特定列的数据自动递增的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆