Mysql database chapter describes the basic application installation

1, chapter introduces database

1.1 What is a database

Database: Saving data warehouse. It reflects our computers, it is a file system. These data are then stored in a special file, and requires the use of a fixed language (SQL language) to manipulate data files.
The technical definition:

Database (Database) in accordance with the data structure to organize, build warehouses on computer storage devices [store and manage data.

Introduction 1.2 Database

We develop applications when all the data in the program, eventually need to be saved to a professional software. These specialized software called database we save the data.
We learn database, not to learn how to develop a database software, we learn how to use databases and operational data records in the database. The database software is developed by a third party company.

1.3 Classification Database

Relational database common non-relational database software:
Oracle: It is a large company Oracle relational database, which is free.
DB2: IBM's database, which is free. SqlServer: Microsoft database. Pay
Sybase: Sybase company. Tools PowerDesign database modeling tool.
MySql: a Swedish company earlier invention, the latter company was acquired by sun, late is Oracle. Java to develop applications using the main database:
MySQL (5.5), the Oracle, the DB2.

1.4 What is a relational database

When developing software, there must be between the data in the software there is a certain relationship, these data need to be stored in the database, but also to maintain the relationship between the data, then you can use those databases directly above. And above all databases are all relational databases.
Describe the relationship between data and stored in the database, according to data while learning if these relational query databases,
relational data: to describe the design of the database, the need to use ER diagrams. Entity relationship
ER: Entity Relationship Diagram.
Entity: can be understood as an object of our Java program. Rectangular (rectangle) in FIG ER indicated. For an entity attribute, we call this entity data, using an elliptical ER diagram in FIG.
Entities and relationships between entities: the use of diamond is shown in FIG ER.

2, mysql installation chapter in linux-

2.1, vmware install linux Notes

2.1.1, remember to turn off the firewall

service iptables stop
chkconfig iptables off(关闭开机自启:所谓的永久关闭防火墙)

2.1.2, to create a unified management directory

mkdir ‐p /exprot/software
mkdir ‐p /export/servers

2.1.3 Software Environment

VMware、crt、centos6.9

2.1.4 Installation Environment

1、VMware软件安装
2、构建虚拟机
3、需要配置Linux(ip,mac地址,hostname,防火墙),就可以通过crt这个客户
端连接进行操作
4、在linux操作系统进行安装msyql‐5.6
说明:因为在linux操作系统中,安装软件的方式主要有3种:1、源码安装
(redis)2、rpm安装
3、yum在线安装(安装MySQL为例)‐‐‐linux联网()

2.2, centos6.9 install mysql

2.2.1 to check that comes with mysql

[root@hadoop‐01 servers]# rpm ‐qa |grep mysql
 mysql‐libs‐5.1.73‐8.el6_8.x86_64

2.2.2, built-in uninstall mysql

[root@hadoop‐01 servers]# rpm ‐e ‐‐nodeps mysql‐libs‐5.1.73‐8.el6_8.x86_64
 [root@hadoop‐01 servers]#

2.2.3, download the installation package mysql
2.2.4, the installation package uploaded to the linux server

rz 上传文件到指定的目录(yum install lrzsz)
/export/software/mysql

2.2.5 installation

rpm ‐ivh *.rpm

2.2.6, see the initialization code

A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER !
You will find that password in '/root/.mysql_secret'.
[root@mysql ~]# cat /root/.mysql_secret
# The random password set for the root user at Wed Aug	8 22:19:00 2018 (local time): xQkcU3kbyuZby1_V
[root@mysql ~]#

2.2.7, mysql start and log

#启动mysql
service mysql start
# 登 录 mysql mysql ‐uroot ‐p
(粘贴密码:xQkcU3kbyuZby1_V)

2.2.8, change passwords

set PASSWORD=PASSWORD('123456');

2.2.9 exit mysql client

mysql>quit

2.2.10, log in with the new password

mysql ‐uroot ‐p 123456(新密码)

2.2.11, remote authorization

grant all privileges on *.* to 'root' @'%' identified by '123456'; flush privileges;

2.2.12, verify successful remote authorization

通过windows的mysql客户端工具连接,是否能连接上,能连接上就授权成功,没有连接上, 说明没有授权成功!

3, mysql- basis OPERATION

3.1, log mysql

mysql ‐uroot ‐p 123456

3.2, quit mysql

mysql>quit

3.3, enter the query

Mysql view the current version number and the current time

SELECT VERSION(), CURRENT_DATE;
mysql> SELECT VERSION(), CURRENT_DATE;
+‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐+
| VERSION() | CURRENT_DATE |
+‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐+
| 5.6.25	| 2018‐08‐08	|
+‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐+
1 row in set (0.32 sec)

mysql sql statement in a case-insensitive

mysql> SELECT VERSION(), CURRENT_DATE;
mysql> select version(), current_date; mysql> SeLeCt vErSiOn(), current_DATE;
mysql> SELECT VERSION(), CURRENT_DATE;
+‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐+
| VERSION() | CURRENT_DATE |
+‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐+
| 5.6.25	| 2018‐08‐08	|
+‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐+
1 row in set (0.00 sec)
mysql> select version(), current_date;
+‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐+
| version() | current_date |
+‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐+
| 5.6.25	| 2018‐08‐08	|
+‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐+
1 row in set (0.00 sec)
mysql> SeLeCt vErSiOn(), current_DATE;
+‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐+
| vErSiOn() | current_DATE |
+‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐+
| 5.6.25	| 2018‐08‐08	|
+‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐+
1 row in set (0.00 sec)

May be a simple calculation (see below)

mysql>SELECT SIN(PI()/4), (4+1)*5;
mysql> SELECT SIN(PI()/4), (4+1)*5;
+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+
| SIN(PI()/4)	| (4+1)*5 |
+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+
| 0.7071067811865475 |	25 |
+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+
1 row in set (0.34 sec)

Multiple statements short, you can write in a row

mysql> SELECT VERSION(); SELECT NOW();
+‐‐‐‐‐‐‐‐‐‐‐+
| VERSION() |
+‐‐‐‐‐‐‐‐‐‐‐+
| 5.6.25	|
+‐‐‐‐‐‐‐‐‐‐‐+
1 row in set (0.00 sec)

+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+
| NOW()	|
+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+
| 2018‐08‐08 23:11:11 |
+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+
1 row in set (0.00 sec)

Can be separated by a comma, multiple lines between a plurality of fields statements end with a semicolon


mysql> SELECT
‐> USER()
‐> ,
‐> CURRENT_DATE;
+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐+
| USER() | CURRENT_DATE |
+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐+
| jon@localhost | 2010‐08‐06 |
+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐+

sql statement written in half, and do not want to perform can add '\ c' at the end of the statement

mysql> select
‐> user()
‐> \c mysql>

3.4, to create and use databases

What to see the current database

mysql> show databases;
+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+
| Database	|
+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+
| information_schema |
| mysql	|
| performance_schema |
| test	|
+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+
4 rows in set (0.07 sec)

Create a database

mysql> CREATE DATABASE menagerie;

Use and switch databases





mysql> USE menagerie Database changed

3.5, create a table and use

See what the current database table

mysql>show tables;

Create a table

mysql> CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20),
‐> species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);

Create a table check whether the same sentence and execution

mysql>show create table pet;

+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
| Table | Create Table
+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
| pet	| CREATE TABLE `pet` (
`name` varchar(20) DEFAULT NULL,
`owner` varchar(20) DEFAULT NULL,
`species` varchar(20) DEFAULT NULL,
`sex` char(1) DEFAULT NULL,
`birth` date DEFAULT NULL,
`death` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐

See table below for details

mysql> desc pet;
+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+
| Field	| Type	| Null | Key | Default | Extra |
+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+
|	name	|	varchar(20)	|	YES	|	|	NULL	|	|
|	owner	|	varchar(20)	|	YES	|	|	NULL	|	|
|	species	|	varchar(20)	|	YES	|	|	NULL	|	|
|	sex	|	char(1)	|	YES	|	|	NULL	|	|
|	birth	|	date	|	YES	|	|	NULL	|	|
|	death	|	date	|	YES	|	|	NULL	|	|

Prepare data

Fluffy Harold cat f 1993‐02‐04 Claws Gwen cat m 1994‐03‐17 Buffy Harold dog f 1989‐05‐13 Fang Benny dog m 1990‐08‐27
Bowser Diane dog m 1979‐08‐31 1995‐07‐29 Chirpy Gwen bird f 1998‐09‐11
Whistler Gwen bird 1997‐12‐09 Slim Benny snake m 1996‐04‐29

3.6, import data table

There are two ways to import data in the table

The first: The above data organized into SQL statements, insert into pet ...
second: import manner by loading data into the file table
1, create a file pet.txt (Note: for each field using the tab key apart, the field is not worth recording \ N
instead)

Fluffy	Harold	cat	f	1993‐02‐04
Claws	Gwen	cat	m	1994‐03‐17
Buffy	Harold	dog	f	1989‐05‐13
Fang	Benny	dog	m	1990‐08‐27
Bowser	Diane	dog	m	1979‐08‐31	1995‐07‐29
Chirpy	Gwen	bird	f	1998‐09‐11
Whistler		Gwen	bird	\N	1997‐12‐09	\N
Slim	Benny	snake	m	1996‐04‐29

2, loading data

mysql> load data local infile '/root/data/pet.txt' into table pet; Query OK, 8 rows affected, 6 warnings (0.06 sec)
Records: 8	Deleted: 0	Skipped: 0	Warnings: 6

3, check whether loaded into

mysql> select *from pet;
+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+
| name	| owner	| species | sex	| birth	| death	|
+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+
|	Fluffy	|	Harold	|	cat	|	f	|	1993‐02‐04	|	NULL	|
|	Claws	|	Gwen	|	cat	|	m	|	1994‐03‐17	|	NULL	|
|	Buffy	|	Harold	|	dog	|	f	|	1989‐05‐13	|	NULL	|
|	Fang	|	Benny	|	dog	|	m	|	1990‐08‐27	|	NULL	|
|	Bowser	|	Diane	|	dog	|	m	|	1979‐08‐31	|	1995‐07‐29	|
|	Chirpy	|	Gwen	|	bird	|	f	|	1998‐09‐11	|	NULL	|
|	Whistler	|	Gwen	|	bird	|	NULL	|	1997‐12‐09	|	NULL	|
|	Slim	|	Benny	|	snake	|	m	|	1996‐04‐29	|	NULL	|
+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+
8 rows in set (0.01 sec)

3.7, data retrieval section

3.7.1, retrieve all the data

mysql> select *from pet;
+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+
| name	| owner	| species | sex	| birth	| death	|
+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+
|	Fluffy	|	Harold	|	cat	|	f	|	1993‐02‐04	|	NULL	|
|	Claws	|	Gwen	|	cat	|	m	|	1994‐03‐17	|	NULL	|
|	Buffy	|	Harold	|	dog	|	f	|	1989‐05‐13	|	NULL	|
|	Fang	|	Benny	|	dog	|	m	|	1990‐08‐27	|	NULL	|
|	Bowser	|	Diane	|	dog	|	m	|	1979‐08‐31	|	1995‐07‐29	|
|	Chirpy	|	Gwen	|	bird	|	f	|	1998‐09‐11	|	NULL	|
|	Whistler	|	Gwen	|	bird	|	NULL	|	1997‐12‐09	|	NULL	|
|	Slim	|	Benny	|	snake	|	m	|	1996‐04‐29	|	NULL	|
+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+
8 rows in set (0.01 sec)

3.7.2, delete all the data in the table

mysql> DELETE FROM pet;
mysql> LOAD DATA LOCAL INFILE '/path/pet.txt' INTO TABLE pet;

3.7.3, update table data for a particular record
in the update table named Bowser's birthday

mysql> UPDATE pet SET birth = '1989‐08‐31' WHERE name = 'Bowser';

3.7.4, a specific line of inquiry
query name for the record Bowser

mysql> SELECT * FROM pet WHERE name = 'Bowser';
+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+
| name | owner | species | sex | birth | death |
+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+
| Bowser | Diane | dog | m | 1989‐08‐31 | 1995‐07‐29 |
+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+

Note: String comparisons are case insensitive! As follows:

mysql> SELECT * FROM pet WHERE name = 'Bowser';
+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+
| name	| owner | species | sex	| birth	| death	|
+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+
| Bowser | Diane | dog	| m	| 1979‐08‐31 | 1995‐07‐29 |
+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+
1 row in set (0.00 sec)

mysql> SELECT * FROM pet WHERE name = 'BowsEr';
+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+
| name	| owner | species | sex	| birth	| death	|
+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+
| Bowser | Diane | dog	| m	| 1979‐08‐31 | 1995‐07‐29 |
+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+
1 row in set (0.00 sec)

mysql> SELECT * FROM pet WHERE name = 'BOWSER';
+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+
| name	| owner | species | sex	| birth	| death	|
+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+
| Bowser | Diane | dog	| m	| 1979‐08‐31 | 1995‐07‐29 |
+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+
1 row in set (0.00 sec)

3.7.4.1, look for a particular query birthday after 1998

mysql> SELECT * FROM pet WHERE birth >= '1998‐1‐1';
+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+
| name | owner | species | sex | birth | death |
+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+
| Chirpy | Gwen | bird | f | 1998‐09‐11 | NULL |
| Puffball | Diane | hamster | f | 1999‐03‐30 | NULL |
+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+

3.7.4.2, multi-criteria query (and | or)

mysql> SELECT * FROM pet WHERE species = 'dog' AND sex = 'f';
+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+
| name | owner | species | sex | birth | death |
+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+
| Buffy | Harold | dog | f | 1989‐05‐13 | NULL |
+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+


mysql> SELECT * FROM pet WHERE species = 'snake' OR species = 'bird';
+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+
| name | owner | species | sex | birth | death |
+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+
| Chirpy | Gwen | bird | f | 1998‐09‐11 | NULL |
| Whistler | Gwen | bird | NULL | 1997‐12‐09 | NULL |
| Slim | Benny | snake | m | 1996‐04‐29 | NULL |
+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+

Execution priority logic parentheses

mysql> SELECT * FROM pet WHERE (species = 'cat' AND sex = 'm')
‐> OR (species = 'dog' AND sex = 'f');
+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+
| name | owner | species | sex | birth | death |
+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+
| Claws | Gwen | cat | m | 1994‐03‐17 | NULL |
| Buffy | Harold | dog | f | 1989‐05‐13 | NULL |
+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+
mysql> SELECT name, birth FROM pet;
+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+
| name | birth |
+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+
| Fluffy | 1993‐02‐04 |
| Claws | 1994‐03‐17 |
| Buffy | 1989‐05‐13 |
| Fang | 1990‐08‐27 |
| Bowser | 1989‐08‐31 |
| Chirpy | 1998‐09‐11 |
| Whistler | 1997‐12‐09 |
| Slim | 1996‐04‐29 |
| Puffball | 1999‐03‐30 |
+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+

Not repeat the query fields you want to use keyword DISTINCT

mysql> SELECT DISTINCT owner FROM pet;
+‐‐‐‐‐‐‐‐+
| owner |
+‐‐‐‐‐‐‐‐+
| Benny |
| Diane |
| Gwen |
| Harold |
+‐‐‐‐‐‐‐‐+

You can use a combination of query conditions specific column

mysql> SELECT name, species, birth FROM pet
‐> WHERE species = 'dog' OR species = 'cat';
+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+
| name | species | birth |
+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+
| Fluffy | cat | 1993‐02‐04 |
| Claws | cat | 1994‐03‐17 |
| Buffy | dog | 1989‐05‐13 |
| Fang | dog | 1990‐08‐27 |
| Bowser | dog | 1989‐08‐31 |
+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+

3.7.5 sort
sort (Keywords: ORDER BY) based on a field

mysql> SELECT name, birth FROM pet ORDER BY birth;
+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+
| name | birth |
+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+
| Buffy | 1989‐05‐13 |
| Bowser | 1989‐08‐31 |
| Fang | 1990‐08‐27 |
| Fluffy | 1993‐02‐04 |
| Claws | 1994‐03‐17 |
| Slim | 1996‐04‐29 |
| Whistler | 1997‐12‐09 |
| Chirpy | 1998‐09‐11 |
| Puffball | 1999‐03‐30 |
+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+

L descending order (desc: descending; asc: ascending order)

mysql> SELECT name, birth FROM pet ORDER BY birth desc;//降序排列mysql> SELECT name, birth FROM pet ORDER BY birth asc ;//升序排列

Multi-column sorted
according to species field ascending order, a descending order according to birth field
Note: ORDER BY species no asc, desc, the default is ascending




mysql> SELECT name, species, birth FROM pet
‐> ORDER BY species, birth DESC;
+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+
| name | species | birth |
+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+
| Chirpy | bird | 1998‐09‐11 |
| Whistler | bird | 1997‐12‐09 |
| Claws | cat | 1994‐03‐17 |
| Fluffy | cat | 1993‐02‐04 |
| Fang | dog | 1990‐08‐27 |
| Bowser | dog | 1989‐08‐31 |
| Buffy | dog | 1989‐05‐13 |
| Puffball | hamster | 1999‐03‐30 |
| Slim | snake | 1996‐04‐29 |
+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+

3.7.6, the date of the calculation
to see how old pet, you can use the function TIMESTAMPDIFF calculation date ()

#查询当前的日期
mysql> select curdate() from pet;
+‐‐‐‐‐‐‐‐‐‐‐‐+
| curdate()	|
+‐‐‐‐‐‐‐‐‐‐‐‐+
| 2018‐08‐09 |
+‐‐‐‐‐‐‐‐‐‐‐‐+

#获取当年的年
mysql> select YEAR('2018‐02‐05') AS YEARS from pet;
+‐‐‐‐‐‐‐+
| YEARS |
+‐‐‐‐‐‐‐+
|	2018 |
+‐‐‐‐‐‐‐+

#获取当年的月
mysql> select month('2018‐02‐05') AS YEARS from pet;
+‐‐‐‐‐‐‐+
| YEARS |
+‐‐‐‐‐‐‐+
|	2 |
+‐‐‐‐‐‐‐+

#获取当年的日
mysql> select day('2018‐02‐05') AS YEARS from pet;
+‐‐‐‐‐‐‐+
| YEARS |
+‐‐‐‐‐‐‐+
|	5 |
+‐‐‐‐‐‐‐+
mysql> SELECT name, birth, CURDATE(),
‐> TIMESTAMPDIFF(YEAR,birth,CURDATE()) AS age
‐> FROM pet;

3.7.7, null and not null values
for some of the types of fields to be checked, it is determined whether the certain field is NULL, or non-NULL

mysql> SELECT name, birth, death,
‐> TIMESTAMPDIFF(YEAR,birth,death) AS age
‐> FROM pet WHERE death IS NOT NULL ORDER BY age;
+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+
| name | birth | death | age |
+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+
| Bowser | 1989‐08‐31 | 1995‐07‐29 | 5 |
+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+

4. Examples

The following is an example of how to solve some common problems of MySQL. First, create a table and import data

CREATE TABLE shop (
article INT(4) UNSIGNED ZEROFILL DEFAULT '0000' NOT NULL, dealer CHAR(20) DEFAULT '' NOT NULL,
price DOUBLE(16,2) DEFAULT '0.00' NOT NULL,
PRIMARY KEY(article, dealer));
INSERT INTO shop VALUES
(1,'A',3.45),(1,'B',3.99),(2,'A',10.99),(3,'B',1.45),
(3,'C',1.69),(3,'D',1.25),(4,'D',19.95);

All data 1, the look-up table

select * from shop;
+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+
| article | dealer | price |
+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+
|	0001	|	A	|	3.45	|
|	0001	|	B	|	3.99	|
|	0002	|	A	|	10.99	|
|	0003	|	B	|	1.45	|
|	0003	|	C	|	1.69	|
|	0003	|	D	|	1.25	|
|	0004	|	D	|	19.95	|
+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+

2, selecting the maximum value or the minimum value of a column

SELECT MAX(article) AS article FROM shop;
+‐‐‐‐‐‐‐‐‐+
| article |
+‐‐‐‐‐‐‐‐‐+
|	4 |
+‐‐‐‐‐‐‐‐‐+
//求某一列的最小值
select min(price) as article from shop;
+‐‐‐‐‐‐‐‐‐+
| article |
+‐‐‐‐‐‐‐‐‐+
|	1.25 |
+‐‐‐‐‐‐‐‐‐+

3.1, the largest filtered entire record data for one field value - relating to the subquery

SELECT article, dealer, price
FROM shop
WHERE price=(SELECT MAX(price) FROM shop);
+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+
| article | dealer | price |
+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+
|	0004 | D	| 19.95 |
+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+

3.2, can also be retrieved by related inquiries

SELECT s1.article, s1.dealer, s1.price
FROM shop s1
LEFT JOIN shop s2 ON s1.price < s2.price WHERE s2.article IS NULL;

SELECT article, dealer, price
FROM shop
ORDER BY price DESC LIMIT 1;

4, and the maximum value of each column, and grouped according to a field - Seeking packet topn

SELECT article, MAX(price) AS price
FROM shop
GROUP BY article;
+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+
| article | price |
+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+
|	0001	|	3.99	|
|	0002	|	10.99	|
|	0003	|	1.69	|
|	0004	|	19.95	|
+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+

5、

SELECT article, dealer, price
FROM shop s1
WHERE price=(SELECT MAX(s2.price) FROM shop s2
WHERE s1.article = s2.article);
+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+
| article | dealer | price |
+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+
|	0001	|	B	|	3.99	|
|	0002	|	A	|	10.99	|
|	0003	|	C	|	1.69	|
|	0004	|	D	|	19.95	|
+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+

Guess you like

Origin blog.csdn.net/kai46385076/article/details/93327973