mysql的基本语句

mysql 的基本语句

sql 语句的基础操作
  1. 简单的查询语句 select
-- * 查询所有的数据
select * from table(表名字, 自己创建的) 

--  查询指定的字段
select 字段名1, 字段名2 from table

-- 可以给字段起别名 ;使用 as 或 空格
select 字段名1 as '新名字' from table
select 字段名1 '新名字1', 字段名2 '新名字2' from table

  1. 插入语句 insert into
--  插入一条数据  值和字段的类型需要对应
insert into table (字段名1, 字段名2, ...) values (1,2, ...)

--  批量插入  一次插入多条数据
insert into table (字段名1, 字段名2, ...) values (1,2, ...),(1,2, ...), (1,2, ...) ....

-- 案例:   insert into t_user (name, age, phone) values ('任家骏', 18, '123465476453'),('杨志勇', 20, '12555576453'),('刘铖浩', 36, '888885476453')
  1. 删除数据 delete
--  删除表中所有的数据
delete from table  

-- 删除数据必须指定具体删除的数据  通过 where  语句指定
delete from table where 条件

  1. 修改数据 update
--  统一修改表中指定的所有字段的值
update table set 字段名=新值 

--  修改数据必须要执行具体修改那条数据
update table set 字段名=新值  where 条件
sql语句的条件语句 where
  1. where 后边是详细的条件 可以是关系运算符 : > < >= <= = != <>(不等)
--  简单条件查询语句
select * from table where 字段名 条件判断
-- 案例: select * from score where grade > 80
  1. 条件语句可以使用 and 和 or 和 in() 进行多个条件查询操作
--  and 是 并且 两边都要满足条件
select * from table where 字段名1 条件 and 字段名2 条件;
-- 案例 select * from score where grade > 80 and c_name = '计算机'

--  or 是 或 只要一边满足即可 但是查询的结果是两个条件的结果的集合
select * from table where 字段名1 条件 or 字段名2 条件;
-- 案例:  select * from score where grade > 90 or c_name = '计算机'


--  in 是 指定范围内满足条件  in(值1, 值2, ...)         
select * from table where 字段名 in(1,2, ...) 
--  案例 select * from score where grade in(70,88)
  1. 聚合函数 avg() 平均数 max() 最大值 min() 最小值 sum() 求和 count() 计数(统计)
--  聚合函数中的参数是 表中的字段名, 根据字段类型进行相应的操作
select max(字段名) as '别名', min(字段名) as '别名', sum(字段名) '别名', avg(字段名) '别名' from score
-- 案例: select max(grade) as '最高分', min(grade) as '最低分', sum(grade) '总分', avg(grade) '平均分' from score
  1. 关键字查询 (模糊查询) like
--  like 关键字 结合 % 和 _ 一起使用 ;
--  % : 代表任意个字符 0个或多个  keywords 是关键字就是你要查询的数据
select * from table where 字段名 like '%keywords%'

--  案例: select * from student where address like '%市%'
--  _ : 代表任意一个字符, _ 一个下划线代表一个字符, __ 两个下划线代表两个字符, 匹配时 字符必须和_下划线的个数保持一致
select * from table where 字段名 like '_keywords___'
-- 案例: select * from student where address like '__市___'

  1. 分页查询 limit
--  limit length  获取指定条数的数据
select * from table limit length;
-- 案例: select * from blian limit 5; 获取5条数据

--  limit start,length  指定起点位置,获取指定条数的数据  分页请求的原理
select * from table limit start, length;
--  select * from blian limit 1,3
  1. 分组 group by
select 字段名 from table group by 字段名;
--  分组 后可以进行统计计数
select 字段名, count(字段名) from table group by 字段名;

  1. 排序 order by
--  order by  字段名 排序规则 
--  排序规则 : desc: 降序   asc: 升序(默认值)
select 字段名 from table order by 字段名 desc/asc
-- 案例: select grade from score order by grade desc
sql 的多表查询
  1. 给表起别名
--  
select1的别名.字段名1,2的别名.字段名1 from table1 别名, table2 别名 where 条件
--  select a.name, b.grade from student a, score b where a.id = b.stu_id
  1. 把一个表查询的结果作为另一个表查询的条件
--  查询李四的计算机成绩
select grade from score where stu_id = (select id from student where name = '李四') and c_name = '计算机'
  1. 针对上边的问题 另一中解决方法 : 等值连接
--  查询李四的计算机成绩
select b.name, a.c_name, a.grade from score a, student b where a.stu_id = b.id and b.name = '李四' and a.c_name = '计算机'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值