2014-03-25 98 views
1

我在查詢中遇到問題。asp.net中的查詢字符串錯誤

錯誤是:{「無效的列名稱總記錄「」}

我有一個名爲upload_news表在此表中存在多條記錄,我想取回按國家數據明智的,其中在不同的國家大於20記錄。

select count(Distinct country) AS TotalRecords, country from upload_news where TotalRecords > 20"; 

回答

2

您需要使用group by和having:

select count(Distinct country) AS TotalRecords, country from upload_news 
group by country 
having count(Distinct country) > 20 
+0

謝謝@Donal它工作。 –

+0

很高興能有所幫助 – Donal

1

嘗試這樣

"SELECT * 
FROM 
(
select count(Distinct country) AS TotalRecords, country from upload_news 
group by country 
) T 
where TotalRecords > 20"; 

因爲總記錄別名列,你不能直接訪問它。

+0

{「因爲它不是在聚合函數或GROUP BY子句中包含列‘upload_news.country’在選擇列表中無效」 } –

+0

@ user3419496你現在可以檢查... –