数据库|mysql教程
MySQL,SQL语句
数据库-mysql教程
led控制的app源码,vscode ayu,ubuntu 不能更新,tomcat 名,sqlite 恢复短信,网站公司用什么服务器,iframe 弹出层插件,前端框架多端封装,python 爬虫代理,php去除最后一个字符,seo实战排名,网站打开空白 重启iis就好了,网页设计模板 div css,拍卖公司建站模板,html页面多面板,外贸订单管理系统代码,java程序连接oracle数据库lzw
A、准备工作
图像扫描识别软件 源码,ubuntu 开放22,tomcat增加连接数,python 爬虫文章,php数据库字典,广东快速seo关键词优化技术lzw
1、安装MySQL数据库
源码转中文,vscode uml插件,新版ubuntu安装3D桌面,tomcat无法kill,sqlite软件批量删除,tom微信公众平台插件,vue属于前端的框架吗,python怎么爬虫搜索图片,php5.4 下载,招聘seo兼职,如何修改网站底部,微信网页点击效果代码,系统界面设计模板lzw
Shell脚本一键部署——源码编译安装MySQL
2、实验准备,数据表配置
mysql -uroot -pshow databases;create database train_ticket;use train_ticket;create table REGION(region varchar(10),site varchar(20));create table FARE(site varchar(20),money int(10),date varchar(15));desc REGION;desc FARE;insert into REGION values (south,changsha);insert into REGION values (south, anchang);insert into REGION values ( orth,eijing);insert into REGION values ( orth, ianjin);insert into FARE values (changsha,1000,-01-30);insert into FARE values ( anchang,700,-01-30);insert into FARE values (eijing,1500,-01-30);insert into FARE values ( ianjin,1200,-01-30);insert into FARE values (eijing,2200,-02-05);select * from REGION;select * from FARE;
B、MySQL 高级(进阶) SQL 语句
1、SELECT
显示表格中一个或数个字段的所有资料
语法:SELECT 字段 FROM 表名
select region from REGION;
2、DISTINCT
不显示重复的资料(去重)
语法:SELECT DISTINCT 字段 FROM 表名
select distinct region from REGION;
3、WHERE
有条件查询
语法:SELECT 字段 FROM 表名 WHERE 条件
select site from FARE where money > 1000;select site from FARE where money < 1000;select site from FARE where money = 1000;
4、AND、OR
and(并且)、or(或者)
语法:SELECT 字段 FROM 表名 WHERE 条件1 ([AND|OR] 条件2)+;
select site from FARE where money > 1000 and (money < 1500);select site,money from FARE where money < 500 or (money = 700);select site,money,date from FARE where money >= 500 and (date < -02-05 and money < 1000);
5、IN
显示已知的值的资料
语法:SELECT 字段 FROM 表名 WHERE 字段 IN (‘值1’,‘值2’,……);
select site,money from FARE where money in (700,1000);
6、BETWEEN
显示两个值范围内的资料
语法:SELECT 字段 FROM 表名 WHERE 字段 BETWEEN ‘值一’ and ‘值二’;
select * from FARE where money between 500 and 1000;
7、通配符、LIKE
通常通配符都是跟LIKE一起使用
%:百分号表示零个、一个或多个字符
_:下划线表示单个字符
LIKE:用于匹配模式来查找资料
语法:SELECT 字段 FROM 表名 WHERE 字段 LIKE ‘模式’;
select * from FARE where site like e%;select site,money from FARE where site like \%jin_;
8、ORDER BY
按关键字排序
语法:SELECT 字段 FROM 表名 [WHERE 条件] ORDER BY 字段 [ASC,DESC];
#ASC:按照升序进行排序,默认的排序方式
#DESC:按照降序进行排序
select * from FARE order by money desc;select date,money from FARE order by money desc;
函数
1、数学函数
select abs(-1),rand(),mod(5,3),power(2,3),round (1.579),round(1.734,2);
select sqrt(9),truncate(1.234,2),ceil(1.2),floor(1.9),greatest(1,2,3,4),least(1,2,3,4);
2、聚合函数
select avg(money) from FARE;select count(money) from FARE;select min(money) from FARE;select max(money) from FARE;select sum(money) from FARE;
3、字符串函数
SELECT TRIM ([ [位置] [要移除的字符串] FROM ] 字符串);
select trim(leading a from anchang);select trim(trailing -- from anchang--);select trim(both -- from --nanchang--);select concat(region,site) from REGION where region = south;select concat(region, ,site) from REGION where region = south;select substr(money,1,2) from FARE;select length(site) from FARE;select replace(site,ji,--) from FARE;select upper(site) from FARE;select lower(HAHAHA);select left(site,2) from FARE;select right(site,3) from FARE;select repeat(site,2) from FARE;select space(2); select strcmp(100,200);select reverse(site) from FARE;
4、| | 连接符
如果sql_mode开启开启了PIPES_AS_CONCAT,”||”视为字符串的连接操作符而非或运算符,和字符串的拼接函数Concat相类似,这和Oracle数据库使用方法一样的
mysql -uroot -puse train_ticket;select region || || site from REGION where region = orth;select site || || money || || date from FARE;
5、GROUP BY
语法:SELECT 字段1,SUM(字段2) FROM 表名 GROUP BY 字段1;
select site,sum(money) from FARE group by site;select site,sum(money),date from FARE group by site order by money desc;select site,count(money),sum(money),date from FARE group by site order by money desc;
6、HAVING
语法:SELECT 字段1,SUM(字段2) FROM 表名 GROUP BY 字段1 HAVING(函数条件);
select site,count(money),sum(money),date from FARE group by site having sum(money) >=700;
7、别名
字段别名、表格别名
语法:SELECT “表格別名”.“字段1” [AS] “字段1別名” FROM “表格名” [AS] “表格別名”;
select RE.region AS reg, count(site) from REGION AS RE group by reg;select FA.site AS si,sum(money),count(money),date AS da from FARE AS FA group by si;
8、子查询
连接表格,在WHERE 子句或HAVING 子句中插入另一个SQL 语句
语法:SELECT 字段1 FROM 表格1 WHERE 字段2 [比较运算符] (SELECT 字段1 FROM 表格2 WHERE 条件)
select A.site,region from REGION AS A where A.site in(select B.site from FARE AS B where money<2000);select B.site,money,date from FARE AS B where site in(select A.site from REGION AS A where region = orth);