SQL如何Count Distinct過的資料

2021-07-09 20:02:59 字數 943 閱讀 7333

distinct:

select distinct t1.*,t2.industryid  from positions t1 ,planteddetails t2, landplanted t3 where t3.landid = t1.id and t2.plantedid = t3.id

and (t1.landtype like '%糧%' or t1.gatherid like '%糧%') and t2.industryid ='1'

直接count會報錯

select count((distinct t1.*,t2.industryid  from positions t1 ,planteddetails t2, landplanted t3) where t3.landid = t1.id and t2.plantedid = t3.id

and (t1.landtype like '%糧%' or t1.gatherid like '%糧%') and t2.industryid ='1'

所以我們可以換個思路

select count(*) from (select distinct t1.*,t2.industryid  from positions t1 ,planteddetails t2, landplanted t3 where t3.landid = t1.id and t2.plantedid = t3.id

and (t1.landtype like '%糧%' or t1.gatherid like '%糧%') and t2.industryid ='1') as total

直接把distinct的結果作為一張表count

這裡as total是必須的 臨時表需要名字 不然會報錯

如何預防SQL注入

以前我出去面試時,人家問我什麼叫sql注入,我很迷芒的告訴人家 我不知道,因為我從來沒有遇到過這類情況。後來隨著sql注入漸入流行,我才發現,其實sql注入是因為程式設計師寫程式時的不良習慣導致的一類漏洞的總稱。其主要原因是程式設計師沒有處理好sql語句中的單引號 例如,在數字型引數中,沒有用程式去...

如何防止SQL注入

一 sql注入攻擊的簡單示例 statement select from users where value a variable 上面這條語句是很普通的一條sql語句,主要實現的功能就是讓使用者輸入乙個員工編號然後查詢這個員工的資訊。但是若這條語句被不法攻擊者改裝過後,就可能成為破壞資料的 如攻擊...

sql如何查詢主鍵

把今天學習到的內容分享一下 select constraint name from information schema.key column usage where table name dtproperties 查詢主鍵名稱 constraint name 是主鍵名稱 exec sp helpc...