MySQL零基础入门--表查询

1.1MySql的软件安装和配置以及数据库理论知识储备

  • MySql的软件安装及服务器配置。http://www.runoob.com/mysql/mysql-install.html
  • 数据库理论知识储备:
    • DB: 数据库,存储数据的容器
    • DBMS:数据库管理系统,又称为数据库软件或数据库产品,用于创建或管理DB
    • SQL:结构化查询语言,用于和数据库通讯的语言,不是某个数据库软件特有的,而是几乎所有的主流数据库软件通用的语言
  • 数据库存储数据的特点:
    • 数据存放到表中,然后表再放到库中
    • 一个库可以有多张表,每张表具有唯一的表名用来标识自己
    • 表中有一个或多个列,列又称为“字段”
    • 表中的每一行数据,称为一条“记录”
  • 常见的数据库管理系统:
    • MySQL、Oracle、DB2、SqlServer
  • MySQL的优点:
    • 开源、免费、成本低
    • 性能高、移植性也好
    • 体积小、便于安装
  • MySQL服务的登录和退出
    • 登录:MySQL 【-h 主机名 -P 端口号】 -u 用户名 -p密码
    • 退出:exit或Ctrl+C

1.2 - MySQL 基础 (一)查询语句

一、基础查询

  • 语法:
    • select 查询列表 from 表名;
  • 特点:
    • 查询列表可以是字段、常量、表达式、函数,也可以是多个
    • 查询结果是一个虚拟表

二、条件查询

  • 语法:
    • select 查询列表 from 表名 where 筛选条件;
  • 筛选条件的分类:
    • 1、简单条件运算符:
      • < 、>、=、 <>、 != 、>=、 <= 、 <=>安全等于

    • 逻辑运算符:
      • and、or、not
    • 模糊查询:
      • like:一般搭配通配符使用,可以判断字符型或数值型
      • 通配符:%任意多个字符,_任意单个字符

三、排序查询:

  • 语法:
    • select 查询列表 from 表名 where 筛选条件 order by 排序列表 【asc | desc】;
  • 特点:
    • asc :升序,如果不写默认升序
    • desc:降序
    • order by的位置一般放在查询语句的最后(除limit语句之外)

四、分组查询:

  • 语法:
    • select 分组函数,分组后的字段 from 表名 【where 筛选条件】group by 分组的字段 【having分组后的筛选】【order by 排序列表】;
  • 特点:
    • 使用关键字:分组前 -> where、 分组后 -> having
    • 筛选的表:分组前 -> 原始表、 分组后 -> 分组后的结果
    • 位置:分组前 -> group by的前面、分组后 -> group by的后面

五、还包括连接查询、子查询、分页查询及联合查询此处不做过多的解释说明,有兴趣的可以去MySQL官网查看其用法

项目一:查找重复的电子邮箱(难度:简单)

要求:创建 email表,并插入如下三行数据

±—±--------+
| Id | Email |
±—±--------+
| 1 | [email protected] |
| 2 | [email protected] |
| 3 | [email protected] |
±—±--------+

编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。
根据以上输入,你的查询应返回以下结果:

±--------+
| Email |
±--------+
| [email protected] |
±--------+

说明:所有电子邮箱都是小写字母。
代码如下:

mysql> show databases;
±-------------------+
| Database |
±-------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
±-------------------+

mysql> create database person;
Query OK, 1 row affected (0.00 sec)
mysql> use person;
Database changed
mysql> create table email(Id int not null primary key,Email varchar(10) not null);
Query OK, 0 rows affected (0.02 sec)
mysql> insert into email(Id,Email) values(1,‘[email protected]’),(2,‘[email protected]’),(3,‘[email protected]’);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from email;
±—±--------+
| Id | Email |
±—±--------+
| 1 | [email protected] |
| 2 | [email protected] |
| 3 | [email protected] |
±—±--------+
3 rows in set (0.00 sec)

mysql> select Email from email having count(*)>1;
±--------+
| Email |
±--------+
| [email protected] |
±--------+
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查询,输出表中所有大国家的名称、人口和面积。
例如,根据上表,我们应该输出:

±-------------±------------±-------------+
| name | population | area |
±-------------±------------±-------------+
| Afghanistan | 25500100 | 652230 |
| Algeria | 37100000 | 2381741 |
±-------------±------------±-------------+

代码如下:

mysql> create table World(name varchar(25) not null,continent varchar(25) not null,area int not null,population int not null,gdp int null);
Query OK, 0 rows affected (0.02 sec)

mysql> insert into World values(‘Afghanistan’, ‘Asia’,652230,25500100,20343000);
Query OK, 1 row affected (0.01 sec)
mysql> insert into World values(‘Albania’, ‘Europe’,28748,2831741,12960000);
Query OK, 1 row affected (0.00 sec)
mysql> insert into World values(‘Algeria’, ‘Africa’,2381741,37100000,188681000);
Query OK, 1 row affected (0.01 sec)
mysql> insert into World values(‘Andorra’, ‘Europe’,468,78115,3712000);
Query OK, 1 row affected (0.00 sec)
mysql> insert into World values(‘Angola’, ‘Africa’,1246700,20609294,100990000);
Query OK, 1 row affected (0.01 sec)

mysql> select * from 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 |
±------------±----------±--------±-----------±----------+
5 rows in set (0.00 sec)

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/PohhetS2/article/details/88931986
今日推荐