2021-06-01至06-02课堂笔记(MySQL数据查询)

0601:

-- 创建数据库
create database if not exists schooldb default charset=utf8;
-- 使用数据库
use schooldb;
-- 创建表
create table studentinfo(
  id int auto_increment primary key,
    `name` varchar(10),
    sex char,
    age int ,
    address varchar(50)
);
-- 插入数据
insert into studentinfo (`name` ,sex ,age ,address ) values ('宋江' , '男' , 30 , "山东"),
                                                            ('旋风' , '男' , 28 , "梁山"),
                                                                                                                        ('花容' , '男' , 18 , "河南"),
                                                                                                                        ('顾三娘' , '女' , 20 , "河北"),
                                                                                                                        ('孙二娘' , '女' , 38 , "湖北"),
                                                                                                                        ('潘金莲' , '女' , 16 , "河北"),
                                                                                                                        ('没注意' , '男' , 48 , "北京");
-- 模糊查询 * 通配符 表示所有
select * from studentinfo;
-- 查询所有 建议写全字段
select id ,`name` , sex ,age , address  from studentinfo;
-- 条件查询 where
select * from studentinfo where sex='女';
select * from studentinfo where age < 20 ;
select * from studentinfo where age is null ;
-- 排序 order by (asc 递增 可省略 ) (desc 递减)
select * from studentionfo order by age asc;
-- 限制显示的条数
select * from studentinfo limit 3 ;
-- 显示索引从四开始的数据,往后的三条数据,索引从0开始计算
select * from studentinfo limit 4,3 ;
-- 复合
select * from studntionfo where sex="男" order by age desc limit 6;
-- 别名 alias  as 可以省略不写
select id as 身份编号 ,`name` as 名字 , sex as 性别 ,age as 年龄 , address  as 地址 from studentinfo;
select id  身份编号 ,`name`  名字 , sex  性别 ,age  年龄 , address   地址 from studentinfo;

-- 与或非
select * from studentinfo where age is null ;
select * from studentinfo where age= 18;
select * from studentinfo where age!= 18;
select * from studentinfo where age<>18;
select * from studentinfo where not (age= 18);
select * from studentinfo where age<= 18 and sex='女';
select * from studentinfo where age>= 20 or sex="男";
-- 去重 distinct 去除重复的数据
select distinct `name` , sex , age , address from studentinfo ;
-- 多重字段参与排序
select * from studentinfo order by age asc , id asc ;

select * from studentinfo order by age asc , id desc ;


0602:MySQL的内置函数

-- 字符串函数:

-- 连接字符串
select concat("hello","world");
-- 求长度
select length("hello");
-- 转为大写
select ucase("xiaoxie");
-- 转为小写
select lcase("DAXIE");
-- 去除左侧空格
select ltrim("  quchu  ");
-- 去除右侧空格
select rtrim("  quchuyou  ");
-- 去除两侧空格
select trim("  quchuliangche  ");
-- 替换
select replace("hello world" , "hello" , "nihao");

-- 日期函数

-- 获取当前日期时间 包含年月日时分秒
select now() 
-- 获取当前年月日
select curdate() 
-- 获取当前时分秒
select curtime()
 -- 获取当前日期时戳
select unix_timestamp() 
-- 将时间戳转换成对应的年月日时间
select from_unixtime("1622513659") 

-- 数学函数

-- 绝对值
select abs("-25") 
-- 向上取整
select ceil("14.5")
-- 向下取整
select floor("16.9")
-- 四舍五入
select round("16.9")
-- 随机数 [0,1)
select rand()
-- 取余
select mod("10","3")

-- 系统函数

-- 当前数据库
select database()
-- 当前用户
select user()
-- 当前mysql版本
select version()

insert into studentinfo (`name` , sex , age , address ) values ("小三", "男", 18 , "老喃");
select last_insert_id() -- 最后一条插入记录的主键id

你可能感兴趣的:(2021-06-01至06-02课堂笔记(MySQL数据查询))