JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » JDBC/SQL討論區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
作者 如何取得同一table不同條件下的總筆數??
sakata





發文: 311
積分: 0
於 2016-09-12 00:36 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
1
2
3
4
5
6
7
8
9
10
11
--分開查詢
SELECT count(*) FROM attendancereport
 
SELECT COUNT(*) FROM attendancereport WHERE source_name = '未刷卡'
 
--子查詢
SELECT
  TOP 1
  (SELECT COUNT(*) FROM attendancereport) AS total1,
  (SELECT COUNT(*) FROM attendancereport WHERE source_name = '未刷卡') AS total2
FROM attendancereport


請教各位前輩,我需要在同一個 table 下,取得不同條件的總筆數

除了分開 select 兩次

和 子查詢外,是否還有更好的方法

感謝 各位


reply to postreply to post
作者 Re:如何取得同一table不同條件下的總筆數?? [Re:sakata]
jimmy14

Give me J-Power !



發文: 787
積分: 3
於 2016-09-12 00:51 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
試試看
1
2
3
select count(*) as total1,
    sum(case when source_name='未刷卡' then 1 else 0 end) as total2
from attendancereport


reply to postreply to post
作者 Re:如何取得同一table不同條件下的總筆數?? [Re:jimmy14]
sakata





發文: 311
積分: 0
於 2016-09-12 01:04 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
感謝你

reply to postreply to post
» JWorld@TW »  JDBC/SQL討論區

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8