集训之MySQL--Day2

No.1 基础语法学习

1. 导入示例数据库

MySQL导入示例数据库 - MySQL教程™

mysql> select city,phone,country from `offices`;
+---------------+------------------+-----------+
| city          | phone            | country   |
+---------------+------------------+-----------+
| San Francisco | +1 650 219 4782  | USA       |
| Boston        | +1 215 837 0825  | USA       |
| NYC           | +1 212 555 3000  | USA       |
| Paris         | +33 14 723 4404  | France    |
| Beijing       | +86 33 224 5000  | China     |
| Sydney        | +61 2 9264 2451  | Australia |
| London        | +44 20 7877 2041 | UK        |
+---------------+------------------+-----------+
7 rows in set (0.34 sec)

2. SQL是什么?MySQL是什么?

SQL是结构化查询语言,是一种用来操作RDBMS的数据库语言,当前关系型数据库都支持使用SQL语言进行操作,也就是说可以通过 SQL 操作 oracle,sql server,mysql,sqlite 等等所有的关系型的数据库。
MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,后来被Sun公司收购,Sun公司后来又被Oracle公司收购,目前属于Oracle旗下产品。

3. 查询语句 SELECT FROM

语句解释
查询所有列:

select * from 表名;

查询指定列:

select 列1,列2,... from 表名;

去重语句
去掉重复的内容,可以调用函数distinct

select distinct 字段名 from 表名;

前N个语句
用limit取前N个语句

-- limit start, count

	-- 限制查询出来的数据个数
	select * from students where gender=1 limit 2;

	-- 查询前5个数据(起始位置,个数)
	select * from students limit 0, 5;

CASE…END判断语句

4. 筛选语句 WHERE

用于筛选数据库中的部分数据。使用where子句对表中的数据筛选,结果为true的行会出现在结果集中。

select * from 表名 where 条件;

比较运算符:
等于: =
大于: >
大于等于: >=
小于: <
小于等于: <=
不等于: != 或 <>
逻辑运算符:
and
or
not

5. 分组语句 GROUP BY

聚集函数:为了快速得到统计数据,如count(*) 等
将查询结果按照1个或多个字段进行分组,字段值相同的为一组。group by可用于单个字段分组,也可用于多个字段分组。
having 条件表达式:用来分组查询后指定一些条件来输出查询结果。
having作用和where一样,但having只能用于group by

6. 排序语句 ORDER BY

为了方便查看数据,可以对数据进行排序

select * from 表名 order by 列1 asc|desc [,列2 asc|desc,...]

asc从小到大排列,即升序
desc从大到小排序,即降序

7. 函数

时间函数
数值函数
字符串函数
MySQL函数

8. SQL注释

‘#’
'–'符号
'/**/ 允许注释跨行

9. SQL代码规范

SQL编程格式的优化建议
SQL Style Guide

No.2 作业

项目一:查找重复的电子邮箱(难度:简单)
创建 email表,并插入如下三行数据
±—±--------+
| Id | Email |
±—±--------+
| 1 | [email protected] |
| 2 | [email protected] |
| 3 | [email protected] |
±—±--------+

编写一个 SQL 查询,查找 email 表中所有重复的电子邮箱。

--创建表
create table email(
	id int unsigned auto_increment not null primary key,
	Email varchar(200)
);

--插入表数据
insert into email values
(0,'[email protected]'),
(0,'[email protected]'),
(0,'[email protected]');

--查询语句
select Emai from email group by Email having count(Email)>1;
    -> ;
+---------+
| Email   |
+---------+
| a@b.com |
+---------+
1 row in set (0.00 sec)

项目二:查找大国(难度:简单)
创建如下 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查询,输出表中所有大国家的名称、人口和面积。

mysql> select name,population,area
    -> from world
    -> where area>3000000
    ->       or (population >25000000 and gdp>20000000);
+-------------+------------+---------+
| name        | population | area    |
+-------------+------------+---------+
| Afghanistan |   25500100 |  652230 |
| Algeria     |   37100000 | 2381741 |
+-------------+------------+---------+
2 rows in set (0.00 sec)

猜你喜欢

转载自blog.csdn.net/Sioen/article/details/88044855