数据库select语句基础

select 语句

查询所有的内容

bash 复制代码
select * from db02.t1;

列出id,name.english列,并指定显示name=li04的行

bash 复制代码
select id,name,english from db02.t1 where name='li04';

select id as '编号',name as '姓名',english as '英语成绩' from db02.t1 where name='li04';   as别名
+--------+--------+--------------+
| 编号 | 姓名 | 英语成绩 |
+--------+--------+--------------+
|      2 | li04   |           99 |
+--------+--------+--------------+

精确定位 = < >

bash 复制代码
select * from t1 where id<3;   列出1,2行
select * from t1 where id>4;   列出 第5行以上
select * from t1 where id>=4; 列出第4行以上
select * from t1 where id<=2; 列出第1

模糊匹配 like '%' '_'

bash 复制代码
select * from db02.t1 where name like '%l%';   
select * from db02.t1 where name like 'li__';
show variables like '%basedir%';   
show variables like '%socket%'; 

正则表达式

bash 复制代码
select * from db02.t1 where name regexp '^l.*' ;
select * from db02.t1 where name regexp '^li[0-9]{1}';  --1次
select * from db02.t1 where name regexp '^li[0-9]?';    --0次或者1次

select user,host,password from mysql.user where host regexp '\^[0-9].\*[0-9]$';
select user,host,password from mysql.user where host regexp '^l.*';

排序

bash 复制代码
order by asc 升序
order by desc 降序

select * from t1 order by math asc;       
select * from t1 order by english desc;

指定某一列做排序

bash 复制代码
select * from t1 where id order by id asc;   
select * from t1 where english order by english desc;

去除重复行 distinct

bash 复制代码
select distinct id from t1 ;

聚合group by

bash 复制代码
select  * from t1 group by id having id<=2;

合并列: concat

bash 复制代码
select concat(user,'@',host) from mysql.user;  把mysql.user表的user和host列合并
select  concat(path1,'/',homedir)  from t3;  

limit 分页

bash 复制代码
select * from t1 limit 0,1;   显示第一行
select * from t1 limit 1,1;   显示第二行
select * from t1 limit 6,1;   显示第七行

select * from t1 limit 3;  显示前三行

select * from t1 limit 4,3; 显示5,6,7行

select name,english from t1 order by english desc limit 3;   把english排序并显示name,english的前三行

运算

bash 复制代码
select name,sum(english) from t1 ;         计算总和
select name,avg(english) from t1 ;          计算平均数
select name,max(english) from t1 ;          计算最大值
select name,min(english) from t1 ;           计算最小值

select name,(english+math) from t1  order by (english+math) desc limit 1;
select name,(english+math) as sum from t1  order by sum desc limit 1;
select name,english from db02.t1 where english in (select max(english) from db02.t1);
select name,english from db02.t1 order by english desc limit 1;

运算符

    • =

select 1 = 1

返回值为1 --表示为真

select 1 = 2

返回值为0 --表示为假

逻辑

and

or

not and

函数

select password('')

select md5('')

select sha1('')

多表查询

bash 复制代码
select t1.id,t1.name,t2.socre from t1,t2 where t1.id=t2.id;

左连接

bash 复制代码
select * from t1 left outer join t2 on t1.id=t2.id;

右连接

bash 复制代码
select * from t1 right outer join t2 on t1.id=t2.id;

内连接

bash 复制代码
select * from t1 inner join t2 on t1.id=t2.id;

纵向连接

bash 复制代码
select * from t1 inion select * from t2;
bash 复制代码
select current_user()   '当前登录数据库的用户名';
select current_time()   '当前的系统时间';
select current_date()   '当前的日期';
select count(*) from t1;   '显示表中总行数' 
select now()  '时间戳';
相关推荐
C++忠实粉丝29 分钟前
Linux环境基础开发工具使用(2)
linux·运维·服务器
Wang's Blog1 小时前
Redis: 集群环境搭建,集群状态检查,分析主从日志,查看集群信息
数据库·redis
康熙38bdc1 小时前
Linux 环境变量
linux·运维·服务器
存储服务专家StorageExpert1 小时前
DELL SC compellent存储的四种访问方式
运维·服务器·存储维护·emc存储
容器( ु⁎ᴗ_ᴗ⁎)ु.。oO1 小时前
MySQL事务
数据库·mysql
大G哥2 小时前
记一次K8S 环境应用nginx stable-alpine 解析内部域名失败排查思路
运维·nginx·云原生·容器·kubernetes
数据龙傲天2 小时前
1688商品API接口:电商数据自动化的新引擎
java·大数据·sql·mysql
长天一色2 小时前
【ECMAScript 从入门到进阶教程】第三部分:高级主题(高级函数与范式,元编程,正则表达式,性能优化)
服务器·开发语言·前端·javascript·性能优化·ecmascript
醉颜凉2 小时前
银河麒麟桌面操作系统修改默认Shell为Bash
运维·服务器·开发语言·bash·kylin·国产化·银河麒麟操作系统
engineer-gxd3 小时前
MySQL 表的操作
mysql