nivacat之单表 sql语句

单表(sql语句)
1.查询所有内容
格式:select  * from 表名 ;    *  表示的所有
案例:select  *  from student2 ;
2.查询部分字段内容
格式:select  字段1,字段2  from  表名
案例:select  name,age  from student2 ;
3.查询的字段设置成别名  as
格式:select  字段名1  as "别名1",字段2 "别名2"  from  表;
案例:select  name as "姓名",age as"年龄"  from student2 ;注意点:as  可写可不写
4.查询内容可以接条件where  +条件
select * from student2 where 字段 “=,!=,>,<,<>,>=,<=”
条件1:=(等于);!=(不等于);>(大于);<(小于),<>(不等于),>=(大于等于);<=(小于等于)
例如:select * from student2 where age>23;

条件2:and(同时满足); or(满足 一个条件,或者多个条件);between...and(在什么范围之间) ;in (在一个范围集内);not  in(不在一个范围集内);is null(为空);is  not null (不为空) 
例如:select * from student2 where math>=70 and age>21; >and
select * from student2 where math>=70 or age>21;
>or
select * from student2 where age between 19 and 24;==>betwen....and
5.order by 排序
a、降序 desc
格式:select  *  from  表名  order by 字段名   desc;
案例:select  *  from   student2  ORDER BY  english desc ;
b·升序asc
可以省略不写select  *  from   student2  ORDER BY  english asc ;
升序select  *  from   student2  ORDER BY  english  ;(省略不写
c、二次排序
(第一次排序存在相同,就排第二个字段)
案例:select  *  from   student2  ORDER BY  english  desc,chinese asc;

6.like 模糊查询% :表示匹配1个字符或多个字符_:下划线表示一个字符
案例:#like 模糊匹配
select  *  from   student2 where chinese LIKE  "8%" # 匹配8开头的分数
select  *  from   student2 where chinese LIKE  "%8" -- 匹配8结尾的分数
select  *  from   student2 where chinese LIKE  "%8%" -- 匹配包含8的数据
select  *  from   student2 where chinese LIKE  "1__" 匹配1开头下划线几下,100(下划线2下)

7.limit 显示指定的行数,
格式:limt (索引位,步长)索引:一个表格中的索引位从0开始,
举例:第一行索引就0步长:显示多少行 
案例:1、select  *  from   student2  limit 2; ==》 显示两行,默认从索引0开始,显示两行
2、select  *  from   student2  limit 1,2; ==》1是显示从第2行开始索引1开始,2是显行
3、先降序在取指定的行数 select  *  from   student2 ORDER BY english  desc  LIMIT 0,3(3行)

8.#max最大值select  max(id)  from  student2  ;

min最小值select  min(id)  from  student2  ;

avg平均值select  avg(id)  from  student2  ;

-- sum   求和select  sum(id)  from  student2  ; 
-- count   统计select  count(id)  from  student2  ; 
-- distinct 去重select  DISTINCT(class)  from  student2  ;

9.分组  group  by
案例:select  class,sum(id) from  student2 group by class

10.having  和where的意思是一样,也是接条件案例:分组后接having 再接条件#GROUP BY

select   count(name)  from   student2 GROUP BY sexselect   sum(english),sex  from   student2 GROUP BY sex#接条件:having ,与where意思,group by 后面接havingselect   sum(english),sex  from   student2 GROUP BY sex HAVING  sum(english) >260;select   sum(english) as s,sex  from   student2 GROUP BY sex HAVING  s >260;
having一般接在group by后面

10.改
格式:UPDATE 表名 set  字段名=新值 where 条件 ;
案例:UPDATE  student2  set  name="xiaohong"  where id =1

posted @ 2026-04-22 22:39  黄成赞  阅读(3)  评论(0)    收藏  举报