如何忽略分组依据 SQL 列中不同数量的起始 0

发布于 2025-01-09 03:06:40 字数 276 浏览 1 评论 0原文

我正在使用以下查询来查找表中的重复数字:

SELECT 
    c.code,
    COUNT(c.code) 
FROM cards c
group by c.code
HAVING COUNT(c.code)>1;

但是列代码在数字之前有不同数量的 0,例如:

0001897
001897
01897
1897

如何使该查询忽略数字之前的所有 0 以将上述所有内容视为重复(分组中的同一行)?

I am using the following query to find repeated numbers in my table:

SELECT 
    c.code,
    COUNT(c.code) 
FROM cards c
group by c.code
HAVING COUNT(c.code)>1;

But the column code has varying numbers of 0s before the numbers, as an example:

0001897
001897
01897
1897

How could I make that query ignore all 0s before the numbers to consider all of the above as repeated (the same row in a group by)?

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

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

发布评论

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

评论(1

飘落散花 2025-01-16 03:06:40

您可以尝试使用 TRIM 函数前导“0”

SELECT 
    TRIM(LEADING '0' FROM c.code),
    COUNT(c.code) 
FROM cards c
group by TRIM(LEADING '0' FROM c.code)
HAVING COUNT(c.code)>1;

SQLFIDDLE

You can try to use TRIM function with LEADING '0'

SELECT 
    TRIM(LEADING '0' FROM c.code),
    COUNT(c.code) 
FROM cards c
group by TRIM(LEADING '0' FROM c.code)
HAVING COUNT(c.code)>1;

SQLFIDDLE

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