学习内容
#学习内容#
1. 导入示例数据库,教程 https://www.yiibai.com/mysql/how-to-load-sample-database-into-mysql-database-server.html
2. SQL是什么?MySQL是什么?
3. 查询语句 SELECT FROM
语句解释
去重语句
前N个语句
CASE...END判断语句
4. 筛选语句 WHERE
语句解释
运算符/通配符/操作符
5. 分组语句 GROUP BY
聚集函数
语句解释
HAVING子句
6. 排序语句 ORDER BY
语句解释
正序、逆序
7. 函数
时间函数
数值函数
字符串函数
8. SQL注释
9. SQL代码规范
[SQL编程格式的优化建议] https://zhuanlan.zhihu.com/p/27466166
[SQL Style Guide] https://www.dsqlstyle.guide/
导入数据库流程
先下载练手数据库:练手
连接本地数据库ubuntu命令行输入:mysql -u账户 -p密码
连接成功创建一个数据库:> CREATE DATABASE IF NOT EXISTS yiibaidb DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
然后选择使用数据库:use yiilaidb;
再导入进去:source 刚才下载解压的绝对路径 我的路径是/home/capture/下载/yiiaidb.sql;
这里注意‘/’是根目录路径,‘./’ 是当前目录
现在就导入成功了查看一下:show tables;查看当前库的所有表;select * from customers; 查看当前表的所有数据;
SQL是查询语法语句,mysql是存储数据库。
select from是查询语句主要三种用法:
1.单行查找select city(列名) from customers;(表名) #查找customers表中的city列所有值
2.多行查找select city,country,phone from customers;#查找customers表中的city,phone列所有值
3.查找表中所有值 select * from customers;
去重:先找到某一列所有值select city from customers;
然后执行去重命令select distinct city from customers;
前N个语句:select city from customers limit 5; #前五行数据
从X开始的前N个语句:select city from customers limit 5(N) offset 4(X-1); #从第五行开始之后的五行数据
case 语句:第一种方法
select
case city #如果city这个值
when 'NYC' then 'ok' #等于NYC 那么输出ok
when 'Pairs' then 'right' #等于pairs 那么输出right
else 'no' #否则输出no
end
from customers;
第二种:
select #注意和第一种city的位置
case
when city='NYC' then 'ok'
when city='Pairs' then 'right'
else 'no'
end
from customers;
where语句即筛选语句基本用法 select * from 表名 where 筛选条件
常用操作符
注意上图红色箭头的像我的DBMS是不支持!>和!<的用法的
select * from customers where city<>'NYC';#匹配所有city的值不是NYC的行
select * from customers where city='NYC';#匹配所有city的值是NYC的行
select * from customers where city!'NYC';#匹配所有city的值不是NYC的行
select * from customers where customerNumber>300;#匹配所有customerNumber大于300的所有值
select * from customers where customerNumber<300;#匹配所有customerNumber小于300的所有值
这里再说一下between语句和is null语句
select * from customers where customerNumber between 299 and 350; #查询299到350行的数据
select * from customers where customerNumber is null; #判断customerNumber的值是否为空
(知识点先欠着)
作业答案:
1.
2.
作业:
#作业#
项目一:查找重复的电子邮箱(难度:简单)
创建 email表,并插入如下三行数据
+----+---------+
| Id | c |
+----+---------+
| 1 | [email protected] |
| 2 | [email protected] |
| 3 | [email protected] |
+----+---------+
编写一个 SQL 查询,查找 Email 表中所有重复的电子邮箱。
根据以上输入,你的查询应返回以下结果:
+---------+
| Email |
+---------+
+---------+
说明:所有电子邮箱都是小写字母。
项目二:查找大国(难度:简单)
创建如下 World 表
+-----------------+------------+------------+--------------+---------------+
| name | continent | area | population | gdp |
+-----------------+------------+------------+--------------+---------------+
| Afghanistan | Asia | 652230 | 25500100 | 20343000 |
| Albania | Europe | 28748 | 2831741 | 12960000 |
| Algeria | Africa | 2381741 | 37100000 | 188681000 |
| Andorra | Europe | 468 | 78115 | 3712000 |
| Angola | Africa | 1246700 | 20609294 | 100990000 |
+-----------------+------------+------------+--------------+---------------+
如果一个国家的面积超过300万平方公里,或者(人口超过2500万并且gdp超过2000万),那么这
编写一个SQL查询,输出表中所有大国家的名称、人口和面积。
例如,根据上表,我们应该输出:
+--------------+-------------+--------------+
| name | population | area |
+--------------+-------------+--------------+
| Afghanistan | 25500100 | 652230 |
| Algeria | 37100000 | 2381741 |
+--------------+-------------+--------------+
项目一
-- 创建表
CREATE TABLE email ( ID INT NOT NULL PRIMARY KEY, Email VARCHAR(255) );
-- 插入数据
INSERT INTO email VALUES('1','[email protected]'); INSERT INTO email VALUES('2','[email protected]'); INSERT INTO email VALUES('3','[email protected]');
项目二
-- 创建表
CREATE TABLE World ( name VARCHAR(50) NOT NULL, continent VARCHAR(50) NOT NULL, area INT NOT NULL, population INT NOT NULL, gdp INT NOT NULL );
-- 插入数据
INSERT INTO World VALUES('Afghanistan','Asia',652230,25500100,20343000); INSERT INTO World VALUES('Albania','Europe',28748,2831741,12960000); INSERT INTO World VALUES('Algeria','Africa',2381741,37100000,188681000); INSERT INTO World VALUES('Andorra','Europe',468,78115,3712000); INSERT INTO World values('Angola','Africa',1246700,20609294,100990000);