SQL语言基础(3)

SQL语言基础(3),第1张

count(字段)

求 select 返回的记录总数

查询学生总数

select count() from studnets;

count可以结合distinct使用,去重后的统计

查询一共有多少个班级 (把班级进行去重后进行统计)

select count(distinct class) from students;

查询女学生的数量

select count() from studnets where sex ='女';

max(字段名)

查询指定字段里的最大值

查询students中的最大年龄

select max(age) from students;

查询女学生最大年龄

select max(age) from studnets where sex '女';

查询1班最大年龄

select max(age) from students where class = '1班';

min(字段)

查询指定字段里的最小值

查询学生中最小年龄

select min(age)  from students;

查询学生中女生最小年龄

select min(age) from studnets where sex ='女';

查询学生中1班最小年龄

select min(age) from students where class ='1班';

sum(字段名)

指定字段的值求和

查询学生的年龄总和

select sum(age) from students;

查询女学生的年龄总和

select sum(age) from students where sex = '女';

查询1班学生的年龄总和

select sum(age) from students where class ='1班';

age(字段)表示求此字段的平均值

查询班级中的平均年龄

select avg(age) from  studnets;

查询班级中女生的平均年龄

select avg(age) from students where sex = '女';

查询班级为1班的平均年龄

select avg(age) from students where class ='1班';

avg的字段中如果有null,null不做为分母计算平均值 (如果所查询的3个值,其中一个值为null,而只能算做两个值求平均值)

扩展练习

查询所有学生的最大年龄,最小年龄,平均年龄;

select max(age),min(age),avg(age) from students;

查询1班有多少个学生

select count() from sutdnets where class = '1班';

查询3班中年龄小于30岁的同学有多少

select count() from students where class ='3班' and age < '30';

group by 字段名

select 聚合函数 from 表名 where 表名 group by 字段 

group by 就是配合聚合函数使用的

分别查询男女同学的数量

select sex,count() from students group by sex;

查询各个年龄段的学生数量

select age,count() from students group age;

查询1班男生、女生的数量

select sex,count() from where class ='1班' students group by sex;

数据分组的方法,统计各个班级学生总数,平均年龄,最大年龄,最小年龄

select class count(),avg(age),max(age),min(age) from students group by class;

统计各个班级的学生总数,平均年龄、最大年龄、最小年龄、但不统计3班的学生,统计结果按班级从小到大排序

select class count(),avg(age),max(age),min(age) from students where class !='3班' group by class order by class desc;

分组以后筛选

用where查询男生总数

select count() from studnets where sex ='男';

用having查询男生总数

select count() from students group by sex having sex ='男';

求班级人数大于3人的班级

select class, count() from students group by class having count()> 3;

Having与where筛选的区别

where是对标的原始数据进行筛选

having是对group by之后已经分过组的数据进行筛选

having可以使用聚合函数 where不能用聚合函数

查询班级总人数大于2人的班级名称以及班级对应的总人数

select class,count() from students group by class having count() > 2;

查询平均年龄大于30岁的班级名称和班级总人数

select class,count() from students group by class having ave(age) > 30;

select from 表名 where 条件 group by 字段 order by 字段 limit start,count 

 语法:limit开始行,获取行数 

limit总是出现在select语句的最后

start代表开始行号,行号从0开始编号

count代表要显示多少行

省略start默认从0开始,从第一行开始

查询前3行学生记录

select from students limit 0,3;

查询从第4条记录开始的三条记录

select from studnets limit 3,3;

当有where或者group by或者order by,limit总是出现在最后

查询年龄最大同学的名字

select name from students order by age desc limt 0,1;

查询年龄最小女同学的信息

select from students where sex = 女 order by age  limit 0,1;

当一张表记录特别多的时候,就需要用到分页显示

已知:每页显示m条数据,求查询第n页数据

公式 limit (n-1)m,m

每页显示4条记录,第3页的结果  

select from students limit 8,4;

每页显示4条记录,第2页的结果

select form students limit 4,4;

已知每页也数,求一张表需要几页显示

    ♦求总页数

    ♦总页数/每页的记录数

    ♦如果结果是整数,那么就是总页数,如果结果有小数,那么就在结果的整数上+1

每页显示5条记录,分别多条select显示每页的记录

第一页  

select from limit 5;

第二页   套用公式 (n-1)m,m  m条数 n页数

select from limit 5,5;

第三页

select  from limit 10,5; 

第四页

select from limit 15,5;

我来回答

select distinct A from C where B<>0

select @@rowcount

-- 系统函数 @@rowcount 表示上一句sql的行数。

楼主 根据你的语句 可以判断 你的数据库是sql server的

不要用系统表 遍历的方法 遍历数据是最影响性能的

首先你这个20个表是固定的 所以你的语句没有问题 20个表你必须都有读一次 这个省不了

所以你的语句就是最简单的了 没有优化的余地

可以在表的结构上下手 20个表应该都有主键id 自动增长

主键本身就含有聚集索引 所以无论你的数据有多少 查询一条数据都是很快的

希望解决了楼主的疑问

1、使用insert方法插入记录

SQLiteDatabase的insert方法的签名为long insert(String table,String nullColumnHack,ContentValues values),这个插入方法的参数说明如下:

table:代表想插入数据的表名。

nullColumnHack:代表强行插入null值的数据列的列名。

values:代表一行记录的数据。

insert方法插入的一行记录使用ContentValues存放,ContentValues类似于Map,它提供了put(String key,Xxx value)(其中key为数据列的列名)方法用于存入数据、getAsXxx(String key)方法用于取出数据。

例如如下语句:

ContentValues values=new ContentValues();

valuesput("name","孙悟空"):

valuesput("age",500);

//返回新添记录的行号,该行号是一个内部直,与主键id无关,发生错误返回-1

long rowid=dbinsert("person_inf",null,values);

2、使用update方法更新数据

SQLiteDatabase的update方法签名为update(String table,ContentValues values,String whereClause,String[] whereArgs),这个更新方法的参数说明如下:

table:代表想要更新数据的表名。

values:代表想要更新的数据。

whereClause:满足该whereClause子句的记录将会被更新。

whereArgs:用于为whereArgs子句传递参数。

例如我们想要更新person_inf表中所有主键大于20的人的人名,可调用如下方法:

ContentValues values=new ContentValues();

//存放更新后的人名

valuesput("name","新人名");

int result=dbupdate("person_inf",values,"_id>",new Integer[]{20});

3、使用delete方法删除记录

SQLiteDatabase的delete方法签名为delete(String table,String whereClause,String[] whereArgs),这个删除的参数说明如下:

table:代表想删除数据的表名。

whereClause:满足该whereClause子句的记录将会被删除。

whereArgs:用于为whereArgs子句传入参数。

删除person_inf表中所有人名以孙开头的记录

int result=dbdelete("person_inf","person_name like ",new String[]{"孙_"});

4、使用query方法查询记录

SQLiteDatabase的query方法签名为Cursor query(boolean distinct,String table,String[] columns,String selection,String[] selectionArgs,String groupBy,String having,String orderBy,String limit),这个query方法的参数说明如下。

distinct:指定是否去除重复记录。

table:执行查询数据的表名。

columns:要查询出来的列名。

selection:查询条件子句。

selectionArgs:用于为selection子句中占位符传入参数值,值在数组中的位置与占位符在语句中的位置必须一致,否则就会有异常。

groupBy:用于控制分组。

having:用于对分组进行过滤。

orderBy:用于对记录进行排序。

limit:用于进行分页。

例如查询出person_inf表中人名以孙开头的数据

Cursor cursor=dbquery("person_inf",new String[]{"_id,name,age"},"name like ",new String []{"孙%"},null,null,"personid desc","5,10");

cursorclose();

Create Table #TMP

(

ID int identity(1,1),

firstBalance int,

lastBalance int

Insert Into #TMP (lastBalance) Values(1)

Insert Into #TMP (lastBalance) Values(3)

Insert Into #TMP (lastBalance) Values(5)

Insert Into #TMP (lastBalance) Values(2)

Insert Into #TMP (lastBalance) Values(9)

Insert Into #TMP (lastBalance) Values(10)

Select  (Select lastBalance From #Tmp C Where ID=(Select Max(ID) From #Tmp A Where AID<#TMPID)) As firstBalance ,lastBalance From #TMP 

Drop Table #TMP

--表需要一个自增的ID,如果没有,给个排序的字段也可!

sql server2000中通过rowcount实现:

set rowcount 5 --表示受影响的行数为5

update 表名

set

where

这样的话如果查询出的结果超过5行,则只更新前5行

最后别忘了把rowcount调回来

set rowcount 0 --置0表示不限制受影响行数

sql server 2005开始 top 后面支持变量了,所以可以这样实现:

declare @n int

set @n=5

update top(@n) 表名

set

where

oracle中通过rowcount实现:

update 表名

set

where rownum<=5

以上就是关于SQL语言基础(3)全部的内容,包括:SQL语言基础(3)、SQL 使用select查询语句返回结果,如何获得结果的数量,即行数!、有1到20个数据表,SQL如何获取每个表最新的第一行数据,组成一个结果集等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/9464414.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-28
下一篇 2023-04-28

发表评论

登录后才能评论

评论列表(0条)

保存