MYSQL语法(模糊查询,视图,索引)
08月11日
|萬仟网IT编程
|我要评论
MYSQL模糊查询模糊查询,查询name 以张开头的数据select *from t1 where name like ‘张%’;查询姓名包含’三’的记录select *from t1 where name like ‘%三%’;查询姓名以‘刚’结尾的两个字符的名字select *from t1 where name like ‘_刚’;查询头两条记录select *from t1 limit 2;查询索引从1开头的3条记录select *from t1 limit
MYSQL
模糊查询
模糊查询,查询name 以张开头的数据
select *from t1 where name like '张%';
查询姓名包含’三’的记录
select *from t1 where name like '%三%';
查询姓名以‘刚’结尾的两个字符的名字
select *from t1 where name like '_刚';
查询头两条记录
select *from t1 limit 2;
查询索引从1开头的3条记录
select *from t1 limit 1,3;
使用offset,索引从1开始的3条记录
select *from t1 limit 3 offset 1;
统计每个年龄有多少人
select age,count(name) as mucount from t1 group by age;
按年龄排序,升序
select *from t1 order by age;
按年龄排序,降序
select *from t1 order by age desc;
别名
select t.id, t.name as myname,t.age as myage from t1 as t;
多表查询
select s.id,s.name,s.tel,s.classid,ame from students as s,class_info as c where s.classid=c.classid;
子查询
select *from students where classid =(select classid from class_info where name ='软件6班');
视图
创建视图
create view view_student as select id,name,birthday,sex from students;
创建视图
create view view_student_classinfo as select s.*,c.name as myname from students as s join class_info as c on s.classid=c.classid;
select *from view_student_classinfo;
修改视图
altel view view_student as select id,name,birthday from students;
查看视图
show tables;
show table status;
5 查看创建视图的信息
show create view view_student;
执行事务
begin;
delete from class_info where class id=6;
delete from students where id=1;
commit;
索引
创建索引
create table t2(id int not null,username varchar(16) not null,index(username));
显示索引
show index from t2;
删除索引
alter table t1 drop index username;
创建唯一约束
create table persons(id int not null ,name varchar(20),address varchar(40)),phone varchar(11) not null unique);
主键
创建主键表
create table city(id int primary key,name varchar(20) not null);
insert into city values(1,'北京'),(2,'哈尔滨'),(3,'上海');
创建外键表student1
create table student1(id int primary key auto_increment,cityid int,foreign key(cityid) references city(id));
insert into student1 values(2,3);
本文地址:/weixin_47440383/article/details/107920440
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!
相关文章:
一,秒杀系统1,秒杀场景电商抢购限量商品抢购演唱会的门票火车票抢座12306…2.为什么要做个系统如果项目流量非...
单表的查询
此表是用户的搜索记录表
key_word : 用户搜索的关键字
total:搜索出来的条数
user_id : 移动端访...
下面通过图文并茂的方式给大家介绍通用设备管理信息系统数据库的创建过程,具体详情请看下文。
设备表:id,名称,类别,型号,投运...
Mysql中limit的用法:在我们使用查询语句的时候,经常要返回前几条或者中间某几行数据,这个时候怎么办呢?不用担心,mysql已经为我们...
1、理解索引本质索引是一个存储 每行数据的磁盘地址 的数据结构:提高查询和更新数据库表的速度全表扫描 and 走...
引子 mysql官方网站上没有 windows mysql5.7 64位版本msi的安装包下载,我们可以通过zip版本解压缩后手动安装配置环境。 m...
Preface How to rescue a dropped or truncated table online?Dropping or trun...
前段时间因为开学 所以到现在才更新 如有不足之处欢迎指出。一、为什么要学习数据库1.1、数据库的好处1.持久化...
字符集是一套文字符号及其编码,比较规则的集合。第一个字符集是ascll(american standard code for information...
对于一些数据量较大的系统,数据库面临的问题除了查询效率低下,还有就是数据入库时间长。特别像报表系统,每天花费在数据导入上的时间可能会...
网友评论
验证码: