5)基本查询语句

1、select语句:
select格式:

select 字段列表 from 数据源 [where 条件表达式] [group by 分组字段[ having 条件表达式]] [ order by 排序字段[asc | desc]]

where字句 用于指定记录的过滤条件,group by 子句用于对检索的数据进行分组;having子句对分组后的数据进行筛选;order by子句对结果集进行排序;

2、使用select子句指定字段列表:

使用表达式:

select version(),now();

 命名别名:

字段或表达式 [as] 别名;as 可以省略;

select version() as 版本号, now() 服务器时间;

 3、基本查询语句:

select 字段列表 from 表名;

查询全部字段:

select * from student;

查询部分字段:

select student_no,student_name,class_no from student;
select student_no 学号,student_name 名字,class_no 班级号,from student;#命名别名
select stu_no 学号,exam_score 考试成绩,regular_score 平时成绩,exam_score*0.8+regular_score*0.2 综合成绩 from exam;

 4、distinct:

去掉重复的选项;

select distinct 字段列表 from 表名;

这儿使用information_schema 系统数据库下的tables表;

use information_schema;
show tables; #展示该数据下所有的表结构
desc tables; #展示表tabls的结构
select table_schema 数据库名,table_type 类型 from tables;

上述四行命令等价于:  #查询其他数据库下的表名,使用 数据库.表名 的形式;

select table_schema 数据库名 from information_schema.tables;
select table_schema 数据库名,table_type 类型 from information_schema.tables;

可以看到有很多重复的选项;

 加上distinct;去掉重复的;

select distinct table_schema 数据库名,table_type 类型 from tables;

 单列和双列比较:只有两项都相同的才算重复;

 5、使用limit 限定返回数据行:

select 字段列表 from 表名 limit [start],length;

start:缺省值为0,表示第一行;

查询前十行:

select table_schema,table_name from tables limit 10;

查询第七页:

select table_schema,table_name from tables limit 60,10;

 

posted @ 2023-05-25 20:14  QianFa01  阅读(33)  评论(0编辑  收藏  举报