当前位置:Gxlcms > 数据库问题 > 数据库(三)

数据库(三)

时间:2021-07-01 10:21:17 帮助过:36人阅读

DQL 查询语句
-- select [列名,列名,列名...](投影) from 表名
-- [where 条件(0个或多个)]
-- [group by 列名](分组)
-- [having 条件](分组过滤)
-- [order by 列名](排序)
-- [limit 序号,长度](截取)

-- 最简单查询
-- *这里是替代符,替代所有的列名
-- select * from t_student;

-- 投影操作(只要查询操作,就会把查询表复制到内存中一份)
-- select student_id,student_name,student_age from t_student;

-- 别名
-- select student_id 编号,student_name 名字,student_age 年龄 from t_student t;
-- select student_id id,student_name name,student_age age from t_student t;

-- mysql特有的给别名的方式
-- select student_id as id,student_name as name,student_age as age from t_student as t;

-- 计算列
-- select student_id,student_name,student_age+10 as age10 from t_student;

-- 字符串的连接 concat函数
-- select concat(student_name,"_",student_id) as name_id from t_student;

-- 排除重复的列 distinct 写在后面的列名都是要计算重复,下这段代码的意思是,要根据student_name和student_age去重
-- select distinct student_name,student_age from t_student;

-- 注意:不能把列名挡在distinct前面
-- 错误的做法:select student_id,distinct student_name,student_age from t_student;

-- 返回限定的行数
-- limit 有两个参数 第一个参数 从哪里开始(0),第二个参数显示几个
-- limit 可以只写一个参数 从0开始,显示几个

-- select * from t_student limit 3; /*从0开始显示3个*/
-- select * from t_student limit 0,3;
-- select * from t_student limit 3,3; /*从3开始(注意limit是从0开始编号的),显示3个*/

-- limit用处很大,我们后面的分页,都需要使用到limit

-- *************************************************************************************

-- 根据条件进行查询
-- select * from t_student where student_age >= 18 and student_age <= 30 and student_sex="男";

-- select * from t_student where student_age between 18 and 30 and student_sex="男";

-- select * from t_student where student_age in (18,19,22,27);

-- select * from t_student where student_age not in (18,19,22,27);

-- like的通配符模糊查找 % _
-- select * from t_student where student_name like ‘%张%‘;

-- 空值的匹配 判断为null is null 判断不为null is not null
-- select * from t_student where student_name is null;

-- 不等于 注意:在mysql中如果是实在没有办法,不要用不等于的判断
-- select * from t_student where student_sex != "男";

-- select * from t_student where student_sex <> "男";


-- *************************************************************************************

-- 排序操作
-- 两种方式
-- 升序 asc 默认
-- 降序 desc

-- 可以根据多个列,进行排序
-- 多列排序要注意,首先根据第一个出现的列进行排序,如果第一个列相等,再根据第二出现的列排序
-- 出现多个列,每个列都要指明是升序还是降序
select * from t_student order by student_age desc,student_id desc;

-- *************************************************************************************
-- 聚合函数
-- max min sum avg count

-- 注意,这里给*的意思是指代整个一行
-- select count(*) as c from t_student;
-- select count(0) as c from t_student; /*count(0)的效率高*/

-- 注意:如果count根据列名去统计,不会统计为null的字段
-- select count(student_name) as c from t_student;

-- select max(student_age) from t_student;
-- select sum(student_age) from t_student;

-- select count(0) as c from t_student where student_sex = "男";

-- 字符串不能求和
-- select sum(student_name) from t_student;

-- 子查询
-- select * from t_student where student_age=(select max(student_age) from t_student);

-- select * from t_student order by student_age desc;

数据库(三)

标签:参数   匹配   avg   子查询   相等   需要   select   between   tween   

人气教程排行