Search Search

#1 worldwide
FREE Coding Lessons

since 1996
   THE BEST WAY to learn ASP & Asp.net!
Advertise Here!
click for details
Credits Host:
DiscountASP.net
Server Admin:
The "Team"
Contact Info.
Charles M. Carroll

my Blog
[prev. Lesson]  SQL: Search AND/OR Examples
     [next Lesson]  SQL: SUM, MIN, AVE, MAX

SQL Count Syntax/Examples

SQL can count items. There are a few variations on the syntax and some simple rules to remember:

  • Any field you want to count here is the simplest syntax
    "select count(*) from publishers where state='NY'
  • If you count a specific field you must groupby that field or it won't work:
    "select count(city),city from publishers group by city"
  • The AS operator allows you to specify a name for the counted field:
    "select count(city) as howmany,city from publishers group by city"

   filename=/learn/test/SQLcount1.asp

<Test Script Below>


<HEAD><TITLE>SQLcount1.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<%
call query2table("select count(*) from publishers where state='NY'")
%>
<!--#include virtual="/learn/test/subdbtable.inc"-->
</BODY></HTML>

   filename=/learn/test/SQLcount2.asp

<Test Script Below>


<HEAD><TITLE>SQLcount2.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<%
call query2table("select count(city),city from publishers group by city")
%>
<!--#include virtual="/learn/test/subdbtable.inc"-->
</BODY></HTML>

   filename=/learn/test/SQLcount3.asp

<Test Script Below>


<HEAD><TITLE>SQLcount3.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<%
call query2table("select count(city) as howmany,city from publishers group by city")
%>
<!--#include virtual="/learn/test/subdbtable.inc"-->
</BODY></HTML>

   filename=/learn/test/SQLcount4.asp

<Test Script Below>


<HEAD><TITLE>SQLcount4.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<%
call query2table("select count(*),city,state from publishers group by city,state")
%>
<!--#include virtual="/learn/test/subdbtable.inc"-->
</BODY></HTML>

The Include file looks like this:

   filename=/learn/test/subdbtable.inc

<Test Script Below>


<% 
sub query2table(inputquery)
    set conntemp=server.createobject("adodb.connection")
    conntemp.open "DSN=Student;uid=student;pwd=magic"
    set rstemp=conntemp.execute(inputquery)
    howmanyfields=rstemp.fields.count -1%>
    <table border=1><tr>
    <% 'Put Headings On The Table of Field Names
    for i=0 to howmanyfields %>
            <td><b><%=rstemp(i).name%></B></TD>
    <% next %>
    </tr>
    <% ' Now lets grab all the records
    do while not rstemp.eof %>
        <tr>
        <% for i = 0 to howmanyfields
            thisvalue=rstemp(i)
            If isnull(thisvalue) then
                thisvalue="&nbsp;"
            end if%>
            <td valign=top><%=thisvalue%></td>
        <% next %>
        </tr>
        <%rstemp.movenext
    loop%>
    </table>
    <%
    rstemp.close
    set rstemp=nothing
    conntemp.close
    set conntemp=nothing
end sub%>

Chaz Wish List
Tall Tip $5
Grande Tip $20
Venti Tip $39
Tip Jar Thanks
2004 Thanks
2005 Thanks
HUGE Tip -love site