MYSQL ROW_NUMBER

发布于 2025-01-10 08:46:55 字数 478 浏览 2 评论 0原文

我正在使用 row_number 函数按分区排名,但我尝试在 IF 语句中使用 row_number 的结果,但没有成功。

我的代码:

ROW_NUMBER() OVER (partition by wboi.ce19.store, dbn.ce19.COD, dbn.ce19.MOD ORDER by dbn.ce19.store, dbn.ce19.data, dbn.ce01.COD) AS ROW1,
IF('ROW1' = 1, 0, 1) as TEST

但是“TEST”列中的所有值都变为 1,即使 row_number 为 1。

此外,我尝试使用 Row_number() 求和,例如:

'ROW1' + 1 AS TEST2

但所有值都是 1。

因此,ROW_NUMBER() 函数输出是一个数字,或者我需要在任何条件函数中使用它之前将其转换为数字?

I'm using row_number function to rank by partition but I'm trying to use the result of row_number in a IF statement without success.

My code:

ROW_NUMBER() OVER (partition by wboi.ce19.store, dbn.ce19.COD, dbn.ce19.MOD ORDER by dbn.ce19.store, dbn.ce19.data, dbn.ce01.COD) AS ROW1,
IF('ROW1' = 1, 0, 1) as TEST

But all the values in "TEST" column are getting 1, even if the row_number is 1.

Also, I tried to make sum with the Row_number() like:

'ROW1' + 1 AS TEST2

But all the values is 1.

So, ROW_NUMBER() function output is a number or I need to convert it to number before use it in any conditional function?

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

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

发布评论

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

评论(1

意中人 2025-01-17 08:46:55

'ROW1' 不是列 ROW1,它是一个 字符串文字

单引号用于分隔字符串。要分隔标识符,请使用反引号。

在数字上下文中使用字符串文字会将字符串转换为其整数值,即前导数字(如果有)或 0。

因此 'ROW1' + 1 等于 0 + 11

除此之外,您不能进行引用同一选择列表中的列别名的 SQL 查询。换句话说,这是一个错误:

mysql> SELECT 1 AS col1, col1+1 AS col2;
ERROR 1054 (42S22): Unknown column 'col1' in 'field list'

您必须将其包装在子查询或 CTE 中:

mysql> SELECT col1+1 AS col2 FROM (SELECT 1 AS col1) AS t;
+------+
| col2 |
+------+
|    2 |
+------+

'ROW1' is not the column ROW1, it's a string literal.

The single-quotes are used to delimit strings. To delimit identifiers, use back-ticks.

Using a string literal in a numeric context converts the string to its integer value, which is the leading digits (if any) or else 0.

So 'ROW1' + 1 is equal to 0 + 1 or 1.

Besides that, you cannot make an SQL query that references a column alias in the same select-list. In other words, this is an error:

mysql> SELECT 1 AS col1, col1+1 AS col2;
ERROR 1054 (42S22): Unknown column 'col1' in 'field list'

You would have to wrap it in a subquery or a CTE:

mysql> SELECT col1+1 AS col2 FROM (SELECT 1 AS col1) AS t;
+------+
| col2 |
+------+
|    2 |
+------+
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文