varchar 的 GROUP BY 问题 [英] GROUP BY problem with varchar

查看:30
本文介绍了varchar 的 GROUP BY 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有链接作为 varchars 存储在数据库中.有了这些链接,我想使用 GROUP BY.

There are links stored in a DB as varchars. With these links I want to use GROUP BY.

http://example.com
http://example.com
http://example.com

针对该数据的 SQL:

SQL over that data:

SELECT COUNT(*) c, Url
    FROM Advertisements
    GROUP BY Url

我想要这个输出:

c Url 
3 http://example.com

但是我得到了三遍:

c Url 
1 http://example.com

为什么 SELECT 不对 varchar 字段进行分组?它们是相同的,但 GROUP BY 没有检测到.有什么想法吗?

Why doesn't SELECT group the varchar fields? They are the same but GROUP BY does not detect that. Any ideas?

推荐答案

结局不同

7 i18704

5 i18704

4 i18704

根据您的评论,我已更新,他们按预期分组.当你尝试这个时,你会得到什么?

Following your comment I have updated and they GROUP as expected. What do you get when you try this?

CREATE TABLE #Advertisements
(
ID INT IDENTITY(1,1),
Url VARCHAR(200)
)

INSERT INTO #Advertisements VALUES
('http://example.com')

INSERT INTO #Advertisements VALUES
('http://example.com')

INSERT INTO #Advertisements VALUES
('http://example.com')



SELECT COUNT(*) c, Url
    FROM #Advertisements
    GROUP BY Url

这篇关于varchar 的 GROUP BY 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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