一、MySQL基本语句知识学习:
二、两个作业:1、查询重复email 2、查询大国
首先连接数据库:>mysql -u root -p
输入密码:******
作业一、查找重复Email
1、创建email_demo数据库:mysql> create database email_demo charset utf8;
2、连接email_demo 数据库:mysql> use email_demo;
3、创建email表:mysql> create table email(
-> Id int not null primary key,
-> Email varchar(100) not null
-> );
4、查看表是否有:mysql> show tables;
±---------------------+
| Tables_in_email_demo |
±---------------------+
| email |
±---------------------+
5、向表中插入数据:
mysql> insert into email(Id,Email)
-> values
-> (1,‘[email protected]’);
或者
mysql> insert into email values(4,‘[email protected]’);
6、查询数据:mysql> select * from email;
±—±--------+
| Id | Email |
±—±--------+
| 1 | [email protected] |
| 2 | [email protected] |
| 3 | [email protected] |
| 4 | [email protected] |
±—±--------+
7、查询重复数据
mysql> SELECT COUNT(*) as repetitions,Email
-> FROM email
-> GROUP BY Email
-> HAVING repetitions > 1;
±------------±--------+
| repetitions | Email |
±------------±--------+
| 2 | [email protected] |
±------------±--------+
或者
mysql> SELECT Email
-> FROM email
-> GROUP BY Email
-> HAVING count(*) > 1;
±--------+
| Email |
±--------+
| [email protected] |
±--------+
作业二、查找大国
1、创建world_demo数据库:mysql> create database world_demo charset utf8;
2、连接world_demo数据库:mysql> use world_demo;
3、创建world表:mysql> create table world(
-> name varchar(100) not null primary key,
-> cntinent varchar(100) not null,
-> area int not null,
-> population int not null,
-> gdp int not null
-> );
4、查看是否有表:mysql> show tables;
±---------------------+
| Tables_in_world_demo |
±---------------------+
| world |
±---------------------+
5、向表中插入数据:mysql> insert into world values(‘Afghanistan’,‘Asia’,652230,25500100,20343000);
Query OK, 1 row affected (0.10 sec)
mysql> insert into world values(‘Albania’,‘Europe’,28748,2831741,12960000);
Query OK, 1 row affected (0.08 sec)
mysql> insert into world values(‘Algeria’,‘Africa’,2381741,37100000,188681000);
Query OK, 1 row affected (0.09 sec)
mysql> insert into world values(‘Andorra’,‘Europe’,468,78115,3712000);
Query OK, 1 row affected (0.11 sec)
mysql> insert into world values(‘Angola’,‘Africa’,1246700,20609294,100990000);
Query OK, 1 row affected (0.03 sec)
6、查询表中数据:mysql> select * from world;
±------------±---------±--------±-----------±----------+
| name | cntinent | 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 |
±------------±---------±--------±-----------±----------+
7、查询满足条件大国
mysql> SELECT name,population,area
-> FROM world
-> GROUP BY name,population,area
-> HAVING population > 250000000;
±------------±-----------±--------+
| name | population | area |
±------------±-----------±--------+
| Afghanistan | 25500100 | 652230 |
| Algeria | 37100000 | 2381741 |
±------------±-----------±--------+