mysql基本操作(重点)
显示数据库
show databases
进入指定数据库
use 数据库名称
- 创建数据库
create database 数据库名称 default character set=utf8
- 删除数据库
drop database 数据库名称
二、数据库表的操作
- 创建表:
create tabale studentinfo( name varchar(10) not null, sex char(10) null, age int(5), phone bigint(11) )studentinfo 为所创建的表名
- 删除表
drop table 表名;
- 新增表数据
# 一次增加一条数据 insert into studentinfo (name,sex,age) values('黎诗','女','12')
# 一次增加多条数据 insert into studentinfo (name,sex,age) values('黎诗','女','21'),('诗诗','女','22')insert into 表名称 (字段名称,多个以,间隔) values (具体的值多个以,间隔)
- 修改
update studentinfo set name = '黎诗' where name = '小诗诗'
- 删除
delete from 表名 where 条件
#拷贝表结构+记录create table day43.user select host,user,password from mysql.user; #只拷贝表结构create table day43.user select host,user,password from mysql.user where 1=2;
拷贝
三、数据库表的简单查询(一个简单小例子)
- 查询所有人员
select * from people
- 只查询人员的姓名和年龄
select p_name,p_age from people
- 查询年龄为20岁的人有哪些?
select * from people where p_age = '20'
- 查询60岁一下的人员有哪些?
select * from people where p_age < '60'查询年龄不等于60岁 的人员 select * from people where p_age <> '60' (推荐,正式)select * from people where p_age != '60' (非常规)常见的逻辑运算符 <, >, =, <=, >=, <>, !=
- 查询50岁以上并且工资大于8000的人员有哪些?(逻辑运算符查询)
select * from people where p_age > 50 && p_sal < 8000# 注意: and (&&)用于连接两个条件 表示并且意思# or(||) 用于连接两个条件表示 或者意思 # 逻辑运算符: = , <, >, !=, <> , <=, >=
- 查询姓张的人员(模糊查询)
select * from people where p_name LIKE '张%', select * from prople wherer p_name LIKE '%张%' # 中间有张字的 # select * from 表名 where 字段 like '%张'# like:表示模糊查询# 以什么开头 : 's%'# 以什么结尾: '%s'# 包含: '%s%'
- 查询哪些人属于武当 华山 嵩山(集合查询)
select * from people where p_menpai = '武当' or p_menpai = '华山' or p_menpai = '嵩山';select * from people where p_menpai in ('武当','华山','嵩山');# select * from 表名 where 字段 in('值1','值2','值3')# in : 表示 集合# not in:表示 反向集合
- 查询工资在5000-8900的人员有哪些?(区间查询)
select * from people where p_sal >= 5000 and p_sal <= 8900;select * from people where p_sal between 5000 and 8900; # select * from 表名 wheere 字段 between 值1 and 值2# 注意 : between...and... 表示区间查询
- 查询所有人员,要求工资倒序排序(排序)
select * from people where p_sal > 3000 ORDER BY p_sal desc # asc 为正序 (默认) # desc 为倒序
- 查询年龄为21岁人员的领导者(嵌套查询)
# select p_learder from people where p_age = '21'# selecr * from people where p_id = 'p003' select * from people where p_id = (selest p_learder from peopple where p_age = '21') # select * from 表 where 字段 in (select 字段 from 表 where id = '值1'# () 优先执行# 遇到 '=' 值唯一,遇到 in 值为集合
- 查询当前人员中谁的工资最高?(嵌套函数)
# select max(p_sal) as p_sal from people select p_name from people where p_sal = (select max(p_sal) as p_sal from people ) # max () 表示最大值 as 表示 起别名
- 查询当前人员中谁的工资最低?
select p_name from people where p_sal = (select min(p_sal) from people) SELECT p_name,p_sal from people where p_sal = (SELECT MIN(p_sal) as p_sal FROM people) # 注意: min 为最小值
- 查询所有人员的平均工资是多少
select avg(p_sal) from people# 注意:avg()表示平均值
- 查询所有人员的工资总和是多少
select sum(p_sal) from ren# 注意 sum()求和
- 查询目前有多少个人员?
select count (p_id) from people# 注意 count(主键) 表示查询表中数据的总条数
- 查询各门派的平均工资是多少
select avg(p_sal),p_menpai,p_name from people GROUP BY p_menpai order by avg(p_sal) desc# 注意: group by 表示分组
- 查询武当派最高工资是谁
select p_name from people where p_sal = (select max(p_sal)from people where p_menpai = '武当') and p_menpai ='武当'
- 查询当前武林中有哪些门派
select p_menpai from people GROUP BY p_menpaiselect distinct p_menpai,p_name from people# 注意 : distinct 表示去重复查询,要求查询的所有字段必须一样,才认为是重复数据
- 查询当前武林中有哪些门派和门派的平均工资是多少
select p_menpai ,avg(p_sal) from people group by p_menpai
- 查询当前人员表的中的第3条数据到第七条数据(分页)
select * from people limit 2,5# 注意 limit 表示分页 # 参数1 : 表示从第几条开始查询,下标从0开始 # 参数2 : 表示每次查询多少条数据
- 查询没有门派的人员有哪些?(条件时 用is null 修改时 用= null)
select * from people where p_menpai is null# 表示查询字段为null 的数据 为 is select * from people where p_menpai = ''# 表示查询字段为 ''的数据# 别的例子 updata people set p_menpai = null where p_id = 'p_008'# 注意 修改字段为null 时,要写 =
- 查询武当派下有哪些小弟
select * from people where p_leader = (select p_id from people where p_menpai = '武当' and p_leader = '0')select * from people where p_mempai = '武当' and p_leader != '0'
- 查询各门派的工资总和按倒序/正序排列
select sum(p_sal) sal,p_menpai from people group by p_menpai order by sal
- 查询人员并显示门派所在位置
select * from people,location where people.p_menpai = location.a_name # 注意: 如果多表联合查询不加条件则会出现(笛卡尔乘积)# : 在使用多表联合查询时,一定要加条件# 结果: 符合两个表条件的结果
- 查询人员表,如果人员门派存在位置则显示位置信息,不存在则不显示位置
select * from people left join location on people.p_menpai = location.a_name# 左连接查询 # 注意:on 表示条件 专门配置 left join 来使用 # 特点:左表数据全要,右表的数据与左表数据相匹配则显示,不匹配则以NULL填充
- 查询位置表,如果人员的门派有位置信息则显示人员,没有则不显示
select * from people right join location on people.p_menpai = location.a_name
- 查询登记了地理位置的门派人员信息
select * from people inner join location on people.p_menpai = location.a_name
转载于:https://www.cnblogs.com/jassin-du/p/8001588.html
mysql基本操作(重点)相关推荐
- MySQL中定义fk语句_MySQL基础篇/第3篇:MySQL基本操作语句.md · qwqoo/MySQL-Review - Gitee.com...
### 第3篇:MySQL基本操作语句 - MySQL基础操作 #### 排序检索数据 - 之前的数据没有进行排序,其是按照默认在数据表中的数据返回的 - SELECT语句的ORDER BY 子句进行 ...
- 20分钟学会mysql_5分钟学会mysql基本操作
mysql视频教程栏目介绍如何快速学会mysql基本操作 相关免费学习推荐:mysql视频教程 文章目录一.SQL是什么? 分类: 二.关于数据库CRUD操作 1.操作表list: 2.对表内数据进行 ...
- mysql键1键2_详解mysql基本操作详细(二)
前言 本文类容 1.数据库的几大约束 2.表与表之间的关系 约束: 主键约束: 作用:为了保证数据的有效性和完整性 mysql中常用的约束:主键约束(primary key) 唯一约束(unique) ...
- linux mysql etc inid_Linux下mysql基本操作
Linux下mysql基本操作 作者:浩浩哥来了 对mysql进行初始密码的添加 方法(一) mysqladmin -uroot password 123 方法(二) 如果在添加初始密码是报错了可以进 ...
- ci mysql操作_MySQL基础篇/第3篇:MySQL基本操作语句.md · icanci/MySQL-Review - Gitee.com...
### 第3篇:MySQL基本操作语句 - MySQL基础操作 #### 排序检索数据 - 之前的数据没有进行排序,其是按照默认在数据表中的数据返回的 - SELECT语句的ORDER BY 子句进行 ...
- MySQL基本操作,个人总结。(WampServer小补充)
原文链接 https://blog.csdn.net/Edogawa_Konan/article/details/80259838 WampServer就是Windows Apache Mysq ...
- 【MySQL基础】MySQL基本操作详解
系列文章目录 第1篇:[MySQL基础]MySQL介绍及安装 第2篇:[MySQL基础]MySQL基本操作详解 文章目录 ✍1,数据库操作
- C++ Mysql基本操作
C++ Mysql基本操作 连接Mysql MYSQL *mysql_init(MYSQL *mysql) 如果mysql是NULL指针,该函数将分配.初始化.并返回新对象.否则,将初始化对象,并返回 ...
- 史上最全MySQL基本操作(这一篇就够用了!!!)
基础知识请移步:数据库.MySQL基本知识 欢迎学习交流!!! 持续更新中- 文章目录 MySQL基本操作 一.SQL语法规则 二.SQL库操作 1.创建数据库 2.显示数据库 3.使用数据库 4.修 ...
- MySQL基本操作(命令行方式)
MySQL基本操作(命令行方式) 1.登录MySQL 2.MySQL 创建数据库 3.MySQL 删除数据库 4.MySQL选择数据库 5.MySQL 创建数据表 6. MySQL 删除数据表 7.M ...
最新文章
- Java单元测试的意义_单元测试重要意义及方法介绍
- 山西出台法规规范社会力量认养文物 系全国首例
- cas跨域单点登录原理_CAS实现SSO单点登录原理
- 生成有控制台的WIN32程序
- db2与oracle的区别 锁,db2和oracle语句区别
- 如何更新linux系统时间
- c语言数据结构课程设计算术表达式求值,《数据结构 课程设计》表达式求值 实验报告...
- 硬盘安装工具cgi_PE系统(U盘安装)
- html设置一个搜索引擎,零基础打造一款属于自己的网页搜索引擎
- 微型计算机补码运算电路特点,二进制数的运算及其加法电路
- 扫拖地机器人预留_进阶规划,扫拖一体,小瓦扫地机器人规划版重度体验
- 静态网页设计——春节
- 打卡 day 8 数组
- 糖尿病性视网膜病变分级诊断
- 删除yum.repos.d
- 超微服务器主板bios装系统,超微主板phoenixbios设置方法
- 关于手机话费充值的方法
- 天津最新建筑施工八大员之(安全员)考试真题及答案解析
- 计算机网络体系结构详解(7层、5层、4层的区别)
- 预测性编码(Predictive Coding)简介
热门文章
- pythonweb开发-pythonWeb开发
- python将数字转变为中文读法-python中将阿拉伯数字转换成中文的实现代码
- 小学生学python-小学生都学Python了,你还不知道如何开始
- LeetCode Longest Palindrome(计算最长的对称串)
- 小端字节序与大端字节序
- Mint-UI 的 DatetimePicker 日期时间插件的安装与使用
- log4j.xml 简介
- 关于过往与未来的思考
- C# 中的委托(Delegate)
- 四.Android adb命令(持续更新...)