增 + 删 + 查 + 改(行操作,表操作)
查
检索 select
返回不重复的项
select distinct name
from class
返回有行数限制
select name
from class
limit 5(前5个数据);// limit 3,4 偏移3行,从第4行开始查询4个数据= limit 4 offset 3
排序检索数据order by,一般order by后面的项是在select中出现的,当然不用select中的也符合语法
select id, name
from student
order by id desc, name #(先对id进行降序排序,再在相同id的时候,对name进行默认升序asc排序)
过滤 where、like(通配符‘_ %’)、regexp
条件操作符(=,!=,<>, <,<=,>,>=,between,is null检查空值)
select id, name
from student
where name='张三' // where age between 10 and 20 // where age >=20 // where phoneNum is null
组合多个where子句(优先级and 》or,还有in, not)
select id, name
from student
where name!='张三' and age>20 // where id=1002 or id=1003 // where id in(1002,1003)
利用通配符进行过滤 ,必须使用like操作符,此外加上%,_,等通配符,使用通配符的搜索一般要比前面的搜索时间长
select name
from student
where name like '张%' #(找出姓张的,张x或张xx,或张xxx等)
若where name like '张_' #(找出张x同学)
使用正则表达式进行搜索
select name
from student
where id regexp '[0-9]{7}' #(7个连续的数字)
//where name regexp '1000 | 2000' #(找出id为1000或2000的,类似or)
//where name regexp '^\. starts? ha$' #(匹配以 . 开头,start的有s或没有s,且以a结尾的的name)
正则表达式regexp与like的区别
- like匹配整个列。如果被匹配的文字在该列中,但like不会找到他,也不会返回该行(除非like + 通配符);
- regexp在列值内进行匹配。使用regexp+ ^|$定位符可以达到匹配列的效果
计算字段
-
什么是计算字段
数据库表中存储的数据并不是自己实际中要用到的数据,需要进行拼接、算数计算等操作,计算字段并不存在于数据库中,而是在select的过程中创建的
字段(计算字段的连接上)==列(用在数据库表中) -
使用计算字段
- 拼接字段(mysql: concat()拼接串; sql: +/ ||)
select concat(trim(name),trim(age)) as info #(将name列和age列数据先去掉前后空格后拼接,且将拼接后的字段命名为info,例如小明23)
from student
+ 执行算术计算
select quantity, item_price, quantity * item_price as expanded_price
from list
函数
- 文本处理函数(left()返回串左边的字符, right()返回右边字符, length()返回串长度, lower()转为小写, upper()大写, ltrim()去左空格, rtrim()去右边空格, trim()去空格, substring()返回子串, soundex()返回串的soundex值)
select name
from students
where soundex(name)=soundex('Y. Lie') #(转换name列值,并搜索出读音类似‘Y.Lie’的值)
- 日期时间处理函数(date(), date_format(), month(), day(), dayofweek(), hour(), now(),year(), time())
select birth_date
from student
where date(birth_date) between '1993-01-01' and '1993-01-03'
#(where year(birth_date)=1993 and month(birth_date)=1
- 数值处理函数(不常用,abs(), cos(), sin(), tan(), exp(), mod(), pi(), rand(), sqrt())
- 聚集函数(处理汇总的数据, avg(), count(), min(), max(), sum())
select avg(age) as avg_age, max(age) as max_age, min(age) as min_age, count(id) as nums
from student
count()或count(*) 计算null值;若指定列名,则不计算null。如count(age)
分组数据(group by 和 having)
- 使用group by的要求(仅在按组计算聚集时使用)
- group by中的列必须是检索列或有效的表达式(但不能是聚集函数)。select中使用表达式,group by中也要使用相同的表达式。group by中不能使用别名
- 除聚集函数外,select语句中的每个列都必须在group by中出现
- 若分组列中有null存在,则null作为一个分组返回。如果有多行null值,将分为一组
- group by分组必须出现在where之后,order by前
- having与where的区别
- where在分组前过滤,having在分组后进行过滤
- where过滤行, having过滤分组
select class_id, sex, count(*) as num
from student
group by class_id, sex
having count(*)>=10
子查询(嵌套在其他查询中的查询)
select id
from order
where order_num in (select order_num from orderitems where pro_id='tnt2')
联结表(联结表会影响sql查询性能)
select v.name, p.name, p.price
from vendors v, products p
where v.vend_id = p.vend_id
#(from vendors v inner join products p on v.vend_id = p.vend_id)
增
数据插入、添加(insert)
insert into student(id, name, age, tel)
values( '12312', '张三', '24', '18212341234'), ('12314','李四', '25', '13712341234');
改
更新数据update,一定要确定更新行的条件where,否则会全部更新
update student
set age='20', tel='18912341234'
where id='12312'
删
删除数据delete,一定要确定删除的条件where,否则会全部删除
delete from student
where id='12312'
表操作(创建,更改,删除)
###创建表create
DROP TABLE IF EXISTS `student`;
create table student
(
id int not null auto_increment,
name char(50) not null,
age char(50) null,
tel char(50) null,
primary key(id)
)ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=utf8;
###更改表alter
alter table student
add sex char(20)
drop column tel;
###删除表(drop)
drop table student