现有一表Products,内有二个字段,分别是:id,Lname例表中数据如下:
id Lname
1 aa
2 aa
3 bb
4 cc
5 cc
6 aa 查询结果:
产品数量统计: aa 有 3台 bb 有1台 cc有两台
请问SQL语句怎么样?数据库是ACCESS
id Lname
1 aa
2 aa
3 bb
4 cc
5 cc
6 aa 查询结果:
产品数量统计: aa 有 3台 bb 有1台 cc有两台
请问SQL语句怎么样?数据库是ACCESS
select Lname,count(1) from tb group by Lname
-- Author:SQL77--RICHIE
-- Version:V1.001 Date:2008-05-15--转Flystone*/-- Test Data: TB
If object_id('TB') is not null
Drop table TB
Go
Create table TB(id int,Lname varchar(2))
Go
Insert into TB
select 1,'aa' union all
select 2,'aa' union all
select 3,'bb' union all
select 4,'cc' union all
select 5,'cc' union all
select 6,'aa'
Go
--Start
Select lname,count(*)as num from TB group by lname
--Result:
/*(所影响的行数为 6 行)lname num
----- -----------
aa 3
bb 1
cc 2(所影响的行数为 3 行)
*/
--End
<!--#include file="conn.asp"--><%
dim rs,num
set rs = server.createobject("adodb.recordset")
sql="SELECT Lname,count(*) as num from Products GROUP BY Lname"
rs.open sql,conn,1,1
if rs.BOF then
response.write "<tr><td colspan='8' width='100%' height='50' align='center'>无相关内容!</td>"
end iffor i=1 to 10
if rs.EOF or rs.BOF then exit for
%>
<table>
<tr bgcolor="#F2F2F2" >
<td height="26" bgcolor="#BAD1FC"><strong>数量统计:<%= rs("Lname") %> <%= num %></strong></td>
</tr>
</table>
<%
rs.movenext
next
%>