show databases; -- 显示连接所有数据库
show tables ; -- 显示当前库所有表
show tables from blog ; -- 显示指定库所有表
show columns from blog; -- 显示表中所有的列
use blog ; -- 使用/打开制定库
/**
语法规范:
不区分大小写
每条命令结尾建议使用分号
注释:
#单行注释
– 单行注释
/ 多行注释
*/
– like 模糊查询
-- name m开头
select * from tag where name like 'm%';
-- name m结尾
select * from tag where name like '%m';
-- name 第二个字符为y
select * from tag where name like '_y%' ;
-- name 第二个字符为_ escape 为定义通配符
select * from tag where name like '_/_y';
select * from tag where name like '_$_%' escape '$';
– in 包含关键字
-- views包含10,9,8
select * from blog where views in (10,9,8);
-- views不包含10,9,8
select * from blog where views not in (10,9,8);
– between and 介于**之间 关键字
-- view 介于 1~10 之间
select * from blog where views between 1 and 10 ;
/*
* * 乘以的意思 IFNULL(userId,0)
* IFNULL(userId,0) userId 如果不为空正常赋值,为空则赋值
*/
select id,views*10*(1+IFNULL(userId,0)) as test from blog where views*10*(1+IFNULL(userId,0)) not between 1 and 1000 ;
– is null / is not null 补充:is 只能跟null , = 跟普通内容
-- id 为null
select * from blog where id is null;
-- id 不为null
select * from blog where id is not null ;
– 排序查询
/**
select 查询列表
from 表名
where 查询条件
order by 排序列表
执行顺序:
1、from 子句
2、where 子句
3、select 子句
4、order by 子句
特点:
1、 排序列表可以是单个字段、多个字段、表达式、函数、列数、以上的最合
2、 升序 asc
降序 desc
*/
– 单个字段排序
-- id 降序
select * from blog a order by a.id desc ;
-- id 升序
select * from blog a order by a.id ;
-- length 函数,统计字数长度
select length(content),content from blog ;
– 多个字段排序
-- 先按照 content 长度升序,再按照 id 降序
select length(content) , id from blog order by length(content) , id desc ;
-- 按照 第6列升序
select * from blog order by 6;
– 补充
-- distinct 去重
select distinct views from blog ;
-- 当前用户
select user();
-- version版本号
select version();
-- CONCAT 拼接字符
select CONCAT(a.ID,a.userId,a.typeId) FROM blog a;
-- length 获取字符长度
select length(content) from blog;