Mysql语句示例
Mysql语句示例
最常用 sql 语句总结
前言
Mysql 是数据库开发使用的主要平台之一。sql 的学习掌握与使用是数据库开发的基础,此处展示详细sql 语句的写法,及各种功能下的 sql 语句。
在此处有 sql 语句使用示例:在这里
此处插入两张图更有说服力:

sql1.PNG

sql2.PNG
说明:
第一张图片是进入该环境,输入自己设定的密码即可进入数据库并进行相关操作;
第二张图片是演示显示所有数据库,设置当前数据库,并对当前数据库操作,显示当前数据库的所有表,查询表中部分记录的命令操作。
基本命令使用是这样的。当然sql 语句也可在其他平台使用。此处不多说明;
SQL语句详细如下:
一、数据库操作
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<span style="font-size: 14px">创建一个名称为mydb1的数据库create database mydb1;show databases;创建一个使用utf-8字符集的mydb2数据库。create database mydb2 character set utf8;创建一个使用utf-8字符集,并带校对规则的mydb3数据库。create database mydb3 character set utf8 collate utf8_general_ci;查看前面创建的mydb2数据库的定义信息show create database mydb2;删除前面创建的mydb1数据库drop database mydb1;查看服务器中的数据库,并把其中某一个库的字符集修改为gb2312;alter database mydb2 character set gb2312;show create database mydb2;使用当前数据库 mydb1,即想对当前数据库进行操作之前使用的命令use mydb1;</span> |
二、表的操作
1>表的创建演示
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<span style="font-size: 14px">创建一个员工表use mydb2;create table employee( id int, name varchar(40), sex varchar(4), birthday date, entry_date date, job varchar(40), salary decimal(8,2), resume text);show tables; 查看库的所有表(查看库里的表要先打开库)show create table employee; 查看表的创建细节desc employee; 看表结构</span> |
2>对表的基本操作:增、删、改、查
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<span style="font-size: 14px">在上面员工表的基本上增加一个image列。alter table employee add image varchar(20);修改job列,使其长度为60。alter table employee modify job varchar(60);删除sex列alter table employee drop sex;表名改为user。rename table employee to user;修改表的字符集为utf-8alter table user character set utf8;列名name修改为usernamealter table test change column address address1 varchar(30)删除表drop table user;</span> |
3>增加、插入记录的 sql 语句详细
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<span style="font-size: 14px">使用insert语句向表中插入三个员工的信息。rename table user to employee;insert into employee(id,username,birthday,entry_date,job,salary,resume) values(1,'aaa','1980-09-09','1980-09-09','bbb',90,'aaaaa');select * from employee;插入数据的细节1insert into employee values(1,'aaa','1980-09-09','1980-09-09','bbb',90,'aaaaa');插入数据的细节2insert into employee values('1','aaa','1980-09-09','1980-09-09','bbb','90','aaaaa');插入数据的细节3(插入中文) 要告诉mysql客户采用gb2312编码 show variables like 'chara%'; set character_set_client=gb2312; insert into employee(id,username) values('3','张三'); 要想查看时不乱码 show variables like 'chara%'; set character_set_results=gb2312; select * from employee;</span> |
4>删除记录的 sql 语句详细
|
1
2
3
4
5
6
7
8
|
<span style="font-size: 14px">删除表中名称为’zs’的记录。delete from employee where username='bbb';删除表中所有记录。delete from employee;使用truncate删除表中记录。truncate table employee;</span> |
5>修改记录的 sql 语句详细
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<span style="font-size: 14px">将所有员工薪水修改为5000元。update employee set salary=5000;将姓名为’bbb’的员工薪水修改为3000元。update employee set salary=3000 where username='bbb';将姓名为’bbb的员工薪水修改为4000元,job改为ccc。update employee set salary=4000,job='ccc' where username='bbb';将bbb的薪水在原有基础上增加1000元。update employee set salary=salary+1000 where username='bbb';更新要注意的问题update employee set username='ccc',salary=9000,birthday='1980-09-09',.....................update where id=1;</span> |
6>查询记录的 sql 语句详细
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
<span style="font-size: 14px">查询表中所有学生的信息。select * from student;查询表中所有学生的姓名和对应的英语成绩。select name,english from student;过滤表中重复的英语数据。select distinct english from student;在所有学生总分上加10分特长分。select name,(chinese+english+math)+10 from student;统计每个学生的总分。select name,(chinese+english+math) from student;关于排序将对象成绩过去统计大于该成绩的人数即可统计数学成绩大于90的学生有多少个?select count(*) from student where math>80;使用别名表示学生分数。select name as 姓名,(chinese+english+math)+10 as 总分 from student;select name 姓名,(chinese+english+math)+10 总分 from student;查询姓名为wu的学生成绩select * from student where name='王五';查询英语成绩大于90分的同学select * from student where english>'90';查询总分大于200分的所有同学select name from student where (chinese+english+math)>200;查询英语分数在 80-90之间的同学。select name from student where english>80 and english<90;select name from student where english between 80 and 90; == select name from student where english>=80 and english<=90;查询数学分数为89,90,91的同学。select * from student where math in(89,90,91);查询所有姓李的学生成绩。select * from student where name like '李%';select * from student where name like '李_';查询数学分>80,语文分>80的同学。select * from student where math>80 and chinese>80;分页查询,查询从第 8 条记录开始的 3 条记录;即:第8 、9 、10 三条记录int from = 2;int end = 10;String sql = "select * from student limit "+from+","+end; //字符串类型的语句select * from student limit 8,3;</span> |
三、对数据记录的操作
查询统计排序等相关处理的 sql 语句
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
<span style="font-size: 14px">对数学成绩排序后输出。select name,math from student order by math; 对总分排序后输出,然后再按从高到低的顺序输出select name 姓名,(chinese+english+math) 总分 from student order by (chinese+english+math) desc;select name 姓名,(chinese+english+math) 总分 from student order by 总分 desc;对姓李的学生成绩排序输出select * from student where name like '李%' order by (chinese+english+math) desc;统计一个班级共有多少学生?select count(name) from student;select count(*) from student;统计数学成绩大于90的学生有多少个?select count(*) from student where math>80;统计总分大于250的人数有多少?select count(*) from student where (chinese+english+math)>250;关于 count的函数的细节 (count只统有值的行)统计一个班级数学总成绩?select sum(math) from student;统计一个班级语文、英语、数学各科的总成绩select sum(chinese),sum(english),sum(math) from student;统计一个班级语文、英语、数学的成绩总和select sum(chinese+english+math) from student;统计一个班级语文成绩平均分select sum(chinese)/count(*) from student;统计一个班级语文成绩平均分select avg(chinese) from student;求一个班级总分平均分select avg(chinese+math+english) from student;求班级最高分和最低分select max(chinese+math+english),min(chinese+math+english) from student;对订单表中商品归类后,显示每一类商品的总价select product,sum(price) from orders group by product;查询购买了几类商品,并且每类总价大于100的商品select product from orders group by product having sum(price)>100;按某一属性对记录进行排序select * from student order by grade desc;</span> |
Mysql语句示例的更多相关文章
- JAVA使用jdbc连接MYSQL简单示例
以下展示的为JAVA使用jdbc连接MYSQL简单示例: import java.sql.DriverManager; import java.sql.ResultSet; import java.s ...
- NET MVC全局异常处理(一) 【转载】网站遭遇DDoS攻击怎么办 使用 HttpRequester 更方便的发起 HTTP 请求 C#文件流。 Url的Base64编码以及解码 C#计算字符串长度,汉字算两个字符 2019周笔记(2.18-2.23) Mysql语句中当前时间不能直接使用C#中的Date.Now传输 Mysql中Count函数的正确使用
NET MVC全局异常处理(一) 目录 .NET MVC全局异常处理 IIS配置 静态错误页配置 .NET错误页配置 程序设置 全局异常配置 .NET MVC全局异常处理 一直知道有.NET有相关 ...
- mysql语句:SET NAMES UTF8
一直以来只知道mysql_query("SET NAMES UTF8");是设定数据库编码的,但是一直不清楚“SET NAMES UTF8”是什么. 直到今天才知道 SET NAM ...
- MySQL安装示例数据库
MySQL安装示例数据库 本文档演示如何下载及安装MySQL示例数据库sakila及employees数据库 1. 安装sakila数据库 1.1 下载sakila数据库 wget http://do ...
- 如何根据执行计划,判断Mysql语句是否走索引
如何根据执行计划,判断Mysql语句是否走索引
- 让dede运行php代码和mysql语句
一.dede运行php代码 举例1: {dede:name runphp='yes'} $str = "hello ";@me = $str;@me .= "world& ...
- php代码优化,mysql语句优化,面试需要用到的
首先说个问题,就是这些所谓的优化其实代码标准化的建议,其实真算不上什么正真意义上的优化,还有一点需要指出的为了一丁点的性能优化,甚至在代码上的在一次请求上性能提升万分之一的所谓就去大面积改变代码习惯, ...
- mysql语句:批量更新多条记录的不同值[转]
mysql语句:批量更新多条记录的不同值 mysql更新语句很简单,更新一条数据的某个字段,一般这样写: 帮助 1 UPDATE mytable SET myfield = 'value' WHERE ...
- Thinkphp用exp表达式执行mysql语句,查询某字段不为空is not null,自动增值
Thinkphp用exp表达式执行mysql语句,查询某字段不为空is not null,自动增值 Thinkphp 的文档经常不够完整的表达MYSQL的各种组合,is not null在thinkp ...
随机推荐
- 【Codevs1227】方格取数2(费用流)
题意:给出一个n*n的矩阵,每一格有一个非负整数Aij,(Aij <= 1000) 现在从(1,1)出发,可以往右或者往下走,最后到达(n,n),每达到一格,把该格子的数取出来,该格子的数就变成 ...
- java多线程总结一:线程的两种创建方式及比较
1.线程的概念:线程(thread)是指一个任务从头至尾的执行流,线程提供一个运行任务的机制,对于java而言,一个程序中可以并发的执行多个线程,这些线程可以在多处理器系统上同时运行.当程序作为一个应 ...
- SQL Server 命令行操作
连接sqlcmd -S localhost -U SA -P '123456'; 执行脚本 sqlcmd -S localhost -U SA -P '123456' -i /root/dbo.sql ...
- 取得mib oidname oid 对应关系表
snmptranslate -Tz -m ALL > d:\2.txt 取得所有名称与OID的对应表,很有用
- 阻塞IO下的echo回显实验
我们只关心代码中的业务逻辑和底层阻塞原因 客户端代码 发送数据 std::string message(len, 'S'); int nw = stream->sendAll(message.c ...
- 【Todo】Java Callable和Future学习
参考这篇文章:http://blog.csdn.net/ghsau/article/details/7451464 还有一个系列<Java多线程>
- error LNK2019 无法解析的外部符号------类模板和内敛函数
今天用类模型实现一个单链表,开始是.h和.cpp将类模板的声明与实现分开写的,结果总是报错: 错误 error LNK2019: 无法解析的外部符号 ?$SingleList@H@@QAE@XZ),该 ...
- 碰撞检測之OBB-OBB的SweepTest
提要 当物体在运动的时候.普通的每帧进行碰撞检測已经无法满足要求,比方子弹的运动 两帧的位置已经直接将中间的板子穿过了,所以 t 时刻和 t +1 时刻的检測都是失效的.这时候须要用到的就是sweep ...
- 生活娱乐 WIFI机器人(某机器发烧友自己动手做一台)
某机器发烧友自己动手做一台WIFI机器人,以下是这位发烧友的自述!让我们一起来分享他的劳动成果-- 在经历了十多天的疯狂淘宝.组装.调试.拆卸.再组装.再调试的过程后,俺的Wifi Robot终于于2 ...
- 第04章-VTK基础(3)
[译者:这个系列教程是以Kitware公司出版的<VTK User's Guide -11th edition>一书作的中文翻译(出版时间2010年,ISBN: 978-1-930934- ...