2015-05-11 79 views
0

下面显示了我所谓的“公司”数据表名如何在SQL中使用DISTINCT命令

enter image description here

这里是我的SQL查询

SELECT 
    name, COUNT(DISTINCT num) AS count 
FROM 
    company 
WHERE 
    (num LIKE '51%' OR num LIKE '65%' OR num LIKE '81%') 
GROUP BY 
    name 

运行后,此查询它显示了以下结果

enter image description here

但我需要bel结果。请帮我解决我的问题。谢谢

enter image description here

+0

为什么您使用的是像运营商的NUM列,它是一个varchar? – Tom

+1

所以你想只用2个第一位数字来计算独特的数字? – Seblor

+0

是的,它是在varchar –

回答

4

我假设你要对列的前2个字符的不同。

您可以使用the function LEFT()此:

查询

SELECT name , COUNT(DISTINCT LEFT(num, 2)) AS count 
FROM new_table 
WHERE (num LIKE '51%' OR num LIKE '65%' OR num LIKE '81%') 
GROUP BY name