加入收藏 | 设为首页 | 会员中心 | 我要投稿 聊城站长网 (https://www.0635zz.com/)- 智能语音交互、行业智能、AI应用、云计算、5G!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

比较常用的一些mysql语句及用法

发布时间:2023-10-12 15:23:03 所属栏目:MySql教程 来源:
导读:本文主要给大家简单讲讲常用的一些mysql语句及用法,相关专业术语大家可以上网查查或者找一些相关书籍补充一下,这里就不涉猎了,我们就直奔主题吧,希望常用的一些mysql语句及用法这篇文章可以给大家带来一些实际帮
本文主要给大家简单讲讲常用的一些mysql语句及用法,相关专业术语大家可以上网查查或者找一些相关书籍补充一下,这里就不涉猎了,我们就直奔主题吧,希望常用的一些mysql语句及用法这篇文章可以给大家带来一些实际帮助。                                                             
 
1.开启mysql数据库服务
 
     格式: net start mysql
 
如: net start mysql57
 
2.关闭mysql数据库服务
 
     格式: net stop mysql
 
     如:  net stop mysql57
 
   
 
   3.登陆mysql服务
 
     格式: mysql -u root -p
 
如:   mysql -u root -p  执行成功后,在输入密码,
 
 
 
  4. 查看当前数据库的版本xinxi
 
格式: select version();
 
常用的一些mysql语句及用法
 
  5.查看当前时间
 
      格式: select now();
 
  6.退出,
 
quit,exit
 
2.数据库database的操作
 
1.查看当前有哪些数据库
 
格式: show databases;
 
2.创建一个数据库
 
格式: create database 数据库名 charset=utf8;
 
例: create database wen charset=utf8;
 
3.删除一个数据库
 
格式: drop database 数据库名;
 
例: drop database wen;
 
4.使用数据库
 
格式:use 数据库名;
 
例: use wen;
 
5.查看当前使用数据库
 
格式: select database();
 
3.表table的操作
 
1.查看当前数据库有哪些表
 
格式: show tables;
 
2.创建一个表
 
格式: create table 表名(字段名及类型);
 
例: create table student(id int auto_increment primary key,name varchar(20) not null,age int not null,address varchar(20),gender bit default 1);
 
3.查看表的结构
 
格式: desc 表名;
 
例: desc student;
 
4.查看创建表的语句
 
格式: show create table 表名;
 
5.删除表
 
格式: drop table 表名;
 
例: drop table student;
 
6.修改表
 
1.修改表的名字
 
格式: rename table 旧表名 to 新表名;
 
例:  rename table student to students;
 
2.修改表的结构, add | drop | change
 
1.add 添加一个新的字段
 
格式: alter table 表名 add 字段名 类型;
 
例:  alter table student add phone int;
 
2.change 修改
 
格式: alter table 表名 change 旧字段名 新字段名 新字段类型;
 
例: alter table student change phone number varchar(20);
 
3.drop 删除
 
格式: alter table 表名 drop 字段名;
 
例: alter table student drop number;
 
4.数据的操作
 
1.插入数据
 
1.插入全列数据
 
格式: insert into 表名 values(值1,值2,....)
 
注意:值得顺序需要与表的结构对应, 主健可以写0  
 
例: insert into student values(0,"小明",36,"北京朝阳区",1);
 
  2.缺省插入
 
   格式:insert into 表名(字段名1,字段名2,....) values(值1,值2,....) 
 
         例: insert into student(name,age,address) values("小东",48,"深圳南山区");
 
3.插入多条数据
 
格式:  insert into 表名 values(值1,值2,....),(值1,值2,....),(值1,值2,..)
 
例:  insert into student values(0,"小云",45,"杭州",0),(0,"小雨",105,"俄罗斯",0),(0,"小清",99,"日本",0);
 
2.删除数据
 
格式: delete from 表名 where 条件
 
例: delete from student where age = 105;
 
delete  from student
 
不加条件表示全部删除, 请谨慎使用
 
3.修改数据
 
格式:update 表名 set 字段名1=值,字段名2=值,...  where 条件;
 
update student set gender = 1 where name = "小青";
 
update student set age = 52,address="深圳" where name = "小东";
 
4.查看所有数据
 
格式: select * from 表名;
 
例: select * from student;
 
5.查询数据
 
1.查询格式
 
1.查询所有
 
格式: select * from 表名;
 
2.按条件查询
 
格式: select 字段名1,字段名2,...  from 表名 where 条件;
 
1.select后跟的是查询结果需要显示的字段名
 
2. * 表示所有的字段名都要显示
 
3. from 后跟的是表名,表示从那个表查
 
4. where 后跟的是筛选条件
 
5. 可以使用 字段名 as 别名   来给字段名取个别名
 
例: 显示 name和age
 
select name,age from student;
 
select name as "姓名" from student;
 
2.条件中的运算
 
1.比较运算符
 
> 大于
 
< 小于
 
>= 大于等于
 
<= 小于等于
 
=  等于
 
!= 不等于
 
格式: select 字段名1,字段名2,...  from 表名 where 字段名 比较运算符  值;
 
例: select * from student where age < 50;
 
2. 逻辑运算符
 
and  且
 
or   或者
 
not   非
 
格式: select 字段名1,字段名2,...  from 表名 where 字段名 比较运算符  值 逻辑运算符 字段名 比较运算符  值 ...;
 
例: select * from student where age < 50 and age > 40;
 
select * from student where not (age < 50 and age > 40);
 
3.模糊运算符 like
 
% 表示多个任意字符
 
_  表示一个任意字符
 
需求: 匹配所有姓小的人
 
格式: select * from student where name like "小%";
 
格式: select * from student where name like "小_";
 
需求: 匹配名字中有 "小"字的
 
      select * from student where name like "%小%";
 
4.范围查询
 
       in (多个值 ) 判断一个值 是否是多个值中的一个
 
       between  值1(包含)  and 值2(包含)   判断一个值是否在 值1与值2之间
 
       格式: select 字段名1,字段名2,...  from 表名 where 字段名 范围运算符  值;
 
      例: 找出  25或者45或者80或者 90  
 
       select * from student where age in (25,45,80,90);
 
       需求:找出25 到 45之间的数据
 
       select * from student where age between 25 and 45;      
 
   5. 空判断
 
       is null 为空
 
       is not null 不为空
 
       格式: select 字段名1,字段名2,...  from 表名 where 字段名 is null;
 
       插入数据: insert into student(name,age) values("小·超",60);
 
例: select * from student where address is null;
 
    select * from student where address is not null;
 
 6.去除重复的值  
 
   格式:  select distinct 字段名 from 表名 where 条件;
 
   例: select distinct gender from student;
 
 7.聚合函数
 
    count(*)   求当前结果总共有多少条数据
 
    sum(列名)  求列名对应列的 和
 
    avg(列名)  求当前列的平均值
 
    max(列名)  求当前列的最大值
 
    min(列名)  求当前列的最小值
 
    例: 求当前表总共有多少条数据?
 
       select count(*) from student;
 
       求年龄最小的?
 
       select min(age) from student;
 
8.分组  group by
 
  格式: select 字段名...  from 表名 where  条件 group by  字段名
 
查看有多少种性别
 
例: select gender from student  group by gender;
 
需求:统计 男生 和  女生 各有多少个
 
        select gender,count(*) from student group by gender;
 
        需求: 统计所有女生的个数?
 
        例: select gender,count(*) from student group by gender having gender = 1;
 
        where 查询条件,   是先执行的查询条件
 
        having 查询条件    是在查询结果的基础上在查询  
 
9. 排序
 
       格式: select 字段名...  from 表名 where 条件 order by 字段名1,字段名2...
 
     例: 年龄小到大
 
      select  * from student  order by age;
 
     默认是从小到大排列
 
      asc  从小到大
 
     desc  从大到小
 
     select  * from student  order by age asc
 
10. 分页
 
格式: select 字段名... from 表名 where 条件  limit 起始值,多少条数据
 
      起始值可以从 0 开始
 
      例: select * from student limit 0,3;
 
 

(编辑:聊城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章