数据蛙数据分析每周作业

数据蛙第一周学习记录

2019-02-24  本文已影响4人  沉默在燃烧

文中加入了自己的一些理解,如有不对请评论指教O(∩_∩)O。
感谢老师的详细讲解和指导。

SQL与MySQL介绍

SQL: 结构化查询语言(Structured Query Language)简称SQL,用于存取数据以及查询、更新和管理关系数据库系统。
MySQL: MySQL是最流行的关系型数据库管理系统。
两者关系:一个是语言一个是系统,可以再MySQL软件下些SQL语句。

数据库基础

  1. 任意两行不具有相同主键
  2. 每一行都必须有一个主键


    pramary key

    如上图,city表中的字段ID作为该表的主键。

SQL介绍

检索数据

  1. SQL不区分大小写
  2. 结束SQL语句需要使用分号(;)
  3. SQL语句会忽略空格
  4. 使用limit 限制返回结果行数
    使用某个库,用use database(库名)
use world;

查询语句,如已制定库,则可以select 字段名 from 表名
select ID,NAME,COUNTRYCODE,DISTRICT,POPULATION from city;
未指定库,则可以select 字段名 from 库名.表名

select ID,NAME,COUNTRYCODE,DISTRICT,POPULATION from world.city;

切换库,直接用use database

use data;

limit的使用,只显示10行,limt 10

select ID,NAME,COUNTRYCODE,DISTRICT,POPULATION from city limit 10;

查看数据库的列表

show databases;

查看表的列表

show tables;

查看表字段信息

show columns from city;
show columns

如上图,int(11)、char(35)等,里面的数字表示最大显示宽度。int最大有效显示宽度是255。显示宽度与存储大小或类型包含的值的范围无关。

  1. desc 降序
  2. asc 升序
  3. order by 位于 from子句之后
use world;
select ID,NAME,COUNTRYCODE,DISTRICT,POPULATION from city order by CountryCode desc,Population asc LIMIT 10;

默认为升序排列(asc),多字段排序有先后,如在CountryCode 排序组内对Population 进行排序。

过滤数据

select ID,NAME,COUNTRYCODE,DISTRICT,POPULATION from world.city where population >= 100000 and population <= 500000 and (countrycode = "NLD" or countrycode = "AFG");

或者用in/not,not in表示不在

select ID,NAME,COUNTRYCODE,DISTRICT,POPULATION from world.city where population >= 100000 and population <= 500000 and countrycode in ("NLD", "AFG");

通配符

  1. “abc%” 以abc开头,任意字符结尾的数据
  2. ‘%abc’ 以abc结尾,任意字符开头的数据
  3. ‘%abc%’ 任意字符开头和结尾,中间包含abc
select ID,NAME,COUNTRYCODE,DISTRICT,POPULATION from city where reverse(NAME) like reverse("%ab");

数据处理函数

  1. 用于处理文本
  2. 数值计算
  3. 处理时间
    length()函数
select ID,NAME,COUNTRYCODE,DISTRICT,POPULATION, length(NAME) as len_name from city; #length()在select内
select ID,NAME,COUNTRYCODE,DISTRICT,POPULATION from city where length(NAME)=6 #length()在where内

substring()、left()、upper()、lower()函数

select ID,NAME,COUNTRYCODE,DISTRICT,POPULATION,substring(NAME,1,3) as sub_name,left(NAME,3) as left_name,upper(NAME) as upper_name,lower(NAME) as lower_name FROM city;

replace()函数

select ID,NAME,COUNTRYCODE,DISTRICT,POPULATION,replace(NAME, "Al", "lA") from city where length(NAME)=6;

日期和时间处理函数

date

date()函数

select date(last_update) from sakila.film where date(last_update)="2006-02-15";

时间差datediff(),比较的是天的差值,parameter1-parameter2

select datediff(now(), last_update) from sakila.film;

汇总数据

select max(population), min(population), avg(population), sum(population) from city;

distinct()需在select后,distinct前面不可以有其他字段,后面可跟多个字段,查找所有字段都不相同的row

select distinct COUNTRYCODE, District from city;

在select语句中可以使用group by子句将行划分成较小的组,然后,使用聚合函数返回每一个组的汇总信息,另外,可以使用having子句限制返回的结果集,因为where关键字无法与聚合函数一起使用。

select COUNTRYCODE,District,avg(population) as avg_popu from city group by CountryCode, District having avg(Population)>=1000000;

子查询

select * from (select COUNTRYCODE,District,avg(population) as avg_popu from city group by CountryCode, District) a where avg_popu>=1000000;

联结表

对多张表进行联结,使用select选取数据
JOIN的方式如下:


join
select a.* from 
(select * from world.city) a 
left join 
(select * from country limit 100) b
on a.countrycode = b.code
left join 
(select * from countrylanguage) c 
on c.countrycode = a.countrycode;

组合查询

  1. 必须由两条或者两条以上的select语句组成,语句之间用union分隔
  2. union中的每个查询必须包含相同的列,表达式或聚集函数
  3. 列数据类型必须兼容

使用union时,返回的字段须一致(数据类型一致,但是容易混乱)。
默认地,UNION 操作符选取不同的值(去重)。如果允许重复的值,请使用 UNION ALL。

select id,name from city where population >= 1000000
union all
select id,name from city where population >= 100000
order by id desc;

创建和操作表

  1. 新表的名字,在关键字CREATE TABLE 之后给出
  2. 表列的名字和定义,用逗号隔开
  1. 表中的一列或者多列可以成为主键,这些值或者组合必须唯一
  2. PRIMARY KEY(col),主键不允许有NULL值
create table if not exists xiaoming_test2
(
id int not null,
name char(40) not null,
countrycode char(5),
population int,
gnp float,
language char(30)
);

更新/删除表

  1. add column char(20)
  2. drop column

插入数据

上一篇 下一篇

猜你喜欢

热点阅读