2010-12-10 74 views
2
select substring(datapath,1,5), COUNT(substring(datapath,1,5)) from batchinfo 
where datapath like '%thc%' 
group by datapath 
having COUNT(substring(datapath,1,5))>1 

我試圖算多少每個子的有在表中,由於某種原因它計算整個字符串。我究竟做錯了什麼?SQL - 比較字符串,而不是整個字符串

+0

你能提供一個簡單的例子嗎? – Unreason 2010-12-10 19:18:33

回答

2

您只需GROUP BY你想算,而不是完整數據路徑的子字符串。也不需要在計數上重複子字符串函數。

select substring(datapath,1,5), COUNT(*) 
    from batchinfo 
    where datapath like '%thc%' 
    group by substring(datapath,1,5) 
    having COUNT(*)>1 
1

嘗試:

select d, count(*) 
from 
(
    select substring(datapath,1,5) d from batchinfo 
    where datapath like '%thc%' 
) 
group by d 
having COUNT(*) > 1 
+0

我喜歡這個,因爲你不重複子字符串 – BlackICE 2010-12-10 19:24:43