MySQL database 3 packet and single-table, multi-table queries

Flashy faded, people lonely than fireworks ...... - Eileen Chang

First, the operation of the supplementary table

select * from table where column names in (value 1, value 2, ...);

Isolated data corresponding value.

1.1null 和 not null

When using a null:

When a table is created we have null null If you need to check out the information corresponding to the need to use select * from table where field name is null;

mysql> create table v1(id int auto_increment primary key,
    -> name varchar(32),email varchar(32))charset=utf8;
Query OK, 0 rows affected (0.70 sec)

mysql> insert into v1(email) values('xxx');
Query OK, 1 row affected (0.07 sec)

mysql> select * from v1;
+----+------+-------+
| id | name | email |
+----+------+-------+
|  1 | NULL | xxx   |
+----+------+-------+
1 row in set (0.00 sec)

mysql> select * from v1 where name is null;
+----+------+-------+
| id | name | email |
+----+------+-------+
|  1 | NULL | xxx   |
+----+------+-------+
1 row in set (0.01 sec)

mysql> select *from v1 where name='';
Empty set (0.00 sec)

Use null will reduce the efficiency of data queries, not recommended, it recommended that the value is empty by default when you create the table.

1.2 When not null

mysql> create table v2(id int auto_increment primary key,
    -> name varchar(32) not null default '',email varchar(32)not null default '')charset=utf8;
Query OK, 0 rows affected (0.44 sec)


mysql> insert into v2(email) values('xxx');
Query OK, 1 row affected (0.06 sec)

mysql> select *from v2 where name='';
+----+------+-------+
| id | name | email |
+----+------+-------+
|  1 |      | xxx   |
+----+------+-------+
1 row in set (0.00 sec)

Second, a single operating table (Import)

2.1 Grouping

Grouping: The mark of a field in the same classification, such as office employee information table packet, or grouped by gender.

2.1.1 aggregate function

max (columns) and the maximum value of the column

min (column) obtain the minimum value column

sum (column) on the column data summation

count (column) data in the column count

avg (column) and calculate the mean of the data in the column

See group by example

2.1.2group by

usage:

Aggregation function select field, the field selected from the employee group by group;

a packet group by the keyword, group by polymerization, and must function (count) one appears. count (field), according to the condition of the data field is counted.

example:

1. gender, for example, are grouped statistics about the number of boys and girls is how many.

mysql> create table employee(
    -> id int not null unique auto_increment primary key,
    -> name varchar(20) not null,
    -> gender enum('male','female') not null default 'male',
    -> age int(3) unsigned not null default 28,
    -> hire_date date not null,
    -> post varchar(50),
    -> post_comment varchar(100),
    -> salary double(15,2),
    -> office int,
    -> depart_id int
    -> )charset=utf8;
Query OK, 0 rows affected (0.61 sec)

mysql> insert into employee(name,gender,age,hire_date,post,salary,office,depart_id) values
    -> ('小张','male',73,'20140701','研发部',3500,401,1),
    -> ('小李','male',28,'20121101','研发部',2100,401,1),
    -> ('小赵','female',18,'20150411','研发部',18000,403,3),
    -> ('歪歪','female',48,'20150311','销售部',3000.13,402,2),
    -> ('丫丫','female',38,'20101101','销售部',2000.35,402,2),
    -> ('丁丁','female',18,'20110312','销售部',1000.37,402,2),
    -> ('小明','male',28,'20160311','运营部',10000.13,403,3),
    -> ('小华','male',18,'19970312','运营部',20000,403,3),
    -> ('小王','female',18,'20130311','运营部',19000,403,3);
Query OK, 9 rows affected (0.09 sec)
Records: 9  Duplicates: 0  Warnings: 0

mysql> select count(id),gender from employee group by gender;
+-----------+--------+
| count(id) | gender |
+-----------+--------+
|         4 | male   |
|         5 | female |
+-----------+--------+
2 rows in set (0.10 sec)

mysql> select gender,count(id) as total from employee group by gender;
#这里可以用as重命名显示的列名
+--------+-------+
| gender | total |
+--------+-------+
| male   |     4 |
| female |     5 |
+--------+-------+
2 rows in set (0.00 sec)

2. sectoral grouping, calculated each department oldest man.

mysql> select depart_id , max(age) from employee group by depart_id;
+-----------+----------+
| depart_id | max(age) |
+-----------+----------+
|         1 |       73 |
|         2 |       48 |
|         3 |       28 |
+-----------+----------+
3 rows in set (0.04 sec)

3. sectoral grouping, each department determined the age of the summation.

mysql> select depart_id,sum(age) from employee group by depart_id;
+-----------+----------+
| depart_id | sum(age) |
+-----------+----------+
|         1 |      101 |
|         2 |      104 |
|         3 |       82 |
+-----------+----------+
3 rows in set (0.00 sec)

4. sector grouping, each department determined the age of the average.

mysql> select depart_id,avg(age) from employee group by depart_id;
+-----------+----------+
| depart_id | avg(age) |
+-----------+----------+
|         1 |  50.5000 |
|         2 |  34.6667 |
|         3 |  20.5000 |
+-----------+----------+
3 rows in set (0.02 sec)

2.1.3having

The data after the group by secondary screening

example

5. sector grouping, each department determined the age averaging elect largest sector average.

mysql> select depart_id,avg(age) from employee group by depart_id having avg(age)>35;
+-----------+----------+
| depart_id | avg(age) |
+-----------+----------+
|         1 |  50.5000 |
+-----------+----------+
1 row in set (0.00 sec)


mysql> select depart_id,avg(age)as pj from employee group by depart_id having pj>35;
+-----------+---------+
| depart_id | pj      |
+-----------+---------+
|         1 | 50.5000 |
+-----------+---------+
1 row in set (0.00 sec)

2.1.4 ascending and descending order

order by field name asc (ascending) desc (descending)

Ascending and descending may be used, such as age desc, id asc simultaneously;
represents: firstly descending age, if age has the same row, on the ascending id.

example

mysql> select * from employee order by age desc,id desc;
+----+--------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
| id | name   | gender | age | hire_date  | post      | post_comment | salary   | office | depart_id |
+----+--------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
|  1 | 小张   | male   |  73 | 2014-07-01 | 研发部    | NULL         |  3500.00 |    401 |         1 |
|  4 | 歪歪   | female |  48 | 2015-03-11 | 销售部    | NULL         |  3000.13 |    402 |         2 |
|  5 | 丫丫   | female |  38 | 2010-11-01 | 销售部    | NULL         |  2000.35 |    402 |         2 |
|  7 | 小明   | male   |  28 | 2016-03-11 | 运营部    | NULL         | 10000.13 |    403 |         3 |
|  2 | 小李   | male   |  28 | 2012-11-01 | 研发部    | NULL         |  2100.00 |    401 |         1 |
|  9 | 小王   | female |  18 | 2013-03-11 | 运营部    | NULL         | 19000.00 |    403 |         3 |
|  8 | 小华   | male   |  18 | 1997-03-12 | 运营部    | NULL         | 20000.00 |    403 |         3 |
|  6 | 丁丁   | female |  18 | 2011-03-12 | 销售部    | NULL         |  1000.37 |    402 |         2 |
|  3 | 小赵   | female |  18 | 2015-04-11 | 研发部    | NULL         | 18000.00 |    403 |         3 |
+----+--------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
9 rows in set (0.00 sec)

2.1.5limit limit output

limit offset ,size

limit starting row index, down the length of the query (index 0 represents the first row)

example

mysql> select * from employee limit 0,3;
+----+--------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
| id | name   | gender | age | hire_date  | post      | post_comment | salary   | office | depart_id |
+----+--------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
|  1 | 小张   | male   |  73 | 2014-07-01 | 研发部    | NULL         |  3500.00 |    401 |         1 |
|  2 | 小李   | male   |  28 | 2012-11-01 | 研发部    | NULL         |  2100.00 |    401 |         1 |
|  3 | 小赵   | female |  18 | 2015-04-11 | 研发部    | NULL         | 18000.00 |    403 |         3 |
+----+--------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
3 rows in set (0.00 sec)

2.1.6 inquiries need to follow the order (important)

select * from table where conditions group by order by Condition Condition Condition having limit condition;

where > group by > having > order by > limit

Third, multi-table operations

Foreign key

The primary key (primary key) is a table in one or more fields, its value is used to uniquely identify a record in the table.

Common key (Common Key) In a relational database, the relationship is established by contact between the same or compatible attribute or group represented. If the two relationships have the same or compatible set of properties or attributes, then the property or property group is known as public key of these two relationships.

If the public key is in a relationship in the primary key , then the public key is called a foreign key relationship to another. Thus, foreign keys represent a relevant connection between the two relationships. Table to the foreign key relationship to another table is called the primary key master table having the foreign key table is called from the main table. Also known as the foreign key foreign key .

Reason for using foreign keys:

1. take up less memory space

2. Only need to modify the data in the main table, we will follow the corresponding modified data from the table

3.1-to-many

It refers to a primary-to-many data tables and data tables from one to many relationship, in the following example, a sector may have multiple employees.

Instructions:

the name of the foreign key constraint foreign key (constrained columns) references a table constraint (constraint field)

mysql> create table department(
    -> id int auto_increment primary key,
    -> name varchar(32) not null default '')
    -> charset utf8;
Query OK, 0 rows affected (0.42 sec)

mysql> insert into department(name) values('研发部'),('运维部'),('前台部'),('小卖部');
Query OK, 4 rows affected (0.06 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> create table userinfo(id int auto_increment primary key,
    -> name varchar(32) not null default '',
    -> depart_id int not null default 1,
    -> constraint fk_user_depart foreign key (depart_id) references department(id))
    -> charset utf8;
Query OK, 0 rows affected (0.39 sec)

mysql> insert into userinfo (name,depart_id) values('xiaozhu',1),('xiaoyu',1),
    -> ('laohe',2),('longge',2),('ludi',3),('xiaoguo',4);
Query OK, 6 rows affected (0.21 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql> select * from userinfo;
+----+---------+-----------+
| id | name    | depart_id |
+----+---------+-----------+
|  1 | xiaozhu |         1 |
|  2 | xiaoyu  |         1 |
|  3 | laohe   |         2 |
|  4 | longge  |         2 |
|  5 | ludi    |         3 |
|  6 | xiaoguo |         4 |
+----+---------+-----------+
6 rows in set (0.00 sec)

mysql> select * from department;
+----+-----------+
| id | name      |
+----+-----------+
|  1 | 研发部    |
|  2 | 运维部    |
|  3 | 前台部    |
|  4 | 小卖部    |
+----+-----------+
4 rows in set (0.00 sec)

mysql> insert into userinfo(name,depart_id) values('xiaozhang',5);#depart_id受department.id的约束
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`test2`.`userinfo`, CONSTRAINT `fk_user_depart` FOREIGN KEY (`depart_id`) REFERENCES `department` (`id`))

#联表查询
mysql> select userinfo.name as uname,department.name as dname from userinfo left
    -> join department on depart_id = department.id;
+---------+-----------+
| uname   | dname     |
+---------+-----------+
| xiaozhu | 研发部    |
| xiaoyu  | 研发部    |
| laohe   | 运维部    |
| longge  | 运维部    |
| ludi    | 前台部    |
| xiaoguo | 小卖部    |
+---------+-----------+
6 rows in set (0.00 sec)



3.2-many

When a primary table refers to-many there are multiple tables from the relationship between each data table is many-between, as shown, may be a boy and girl plurality date, and may be a multi-girl a boy appointment.

mysql> create table boy(id int auto_increment primary key,
    -> bname varchar(32) not null default'')charset utf8;
Query OK, 0 rows affected (0.36 sec)

mysql> insert into boy(bname) values('zhangsan'),('lisi'),('wangwu');
Query OK, 3 rows affected (0.09 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> create table girl(id int auto_increment primary key,
    -> gname varchar(32) not null default'')charset utf8;
Query OK, 0 rows affected (0.33 sec)

mysql> insert into girl(gname) values('xiaoli'),('xiaohua'),('xiaomei');
Query OK, 3 rows affected (0.06 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> create table boy_girl(id int auto_increment primary key,
    -> bid int not null default 1,
    -> gid int not null default 1,
    -> constraint fk_boy_girl_boy foreign key(bid) references boy(id),
    -> constraint fk_boy_girl_girl foreign key(gid) references girl(id)
    -> )charset utf8;
Query OK, 0 rows affected (0.42 sec)

mysql> insert into boy_girl(bid,gid) values(1,1),(2,2),(3,3),(3,1),(2,1),(1,2);
Query OK, 6 rows affected (0.04 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql> select * from boy left join boy_girl on boy.id = boy_girl.bid left join girl
    -> on girl.id = boy_girl.gid;
+----+----------+------+------+------+------+---------+
| id | bname    | id   | bid  | gid  | id   | gname   |
+----+----------+------+------+------+------+---------+
|  1 | zhangsan |    1 |    1 |    1 |    1 | xiaoli  |
|  2 | lisi     |    5 |    2 |    1 |    1 | xiaoli  |
|  3 | wangwu   |    4 |    3 |    1 |    1 | xiaoli  |
|  1 | zhangsan |    6 |    1 |    2 |    2 | xiaohua |
|  2 | lisi     |    2 |    2 |    2 |    2 | xiaohua |
|  3 | wangwu   |    3 |    3 |    3 |    3 | xiaomei |
+----+----------+------+------+------+------+---------+
6 rows in set (0.03 sec)

mysql> select bname,gname from boy left join boy_girl on boy.id = boy_girl.bid
    -> left join girl on girl.id = boy_girl.gid;
+----------+---------+
| bname    | gname   |
+----------+---------+
| zhangsan | xiaoli  |
| lisi     | xiaoli  |
| wangwu   | xiaoli  |
| zhangsan | xiaohua |
| lisi     | xiaohua |
| wangwu   | xiaomei |
+----------+---------+
6 rows in set (0.00 sec)
                

3.3 One to One

One refers to data in the two tables is one relationship, using the unique (field names) to restrain this relationship. Since sensitive information belonging to employees wages, a separate table to store, employee information and time wages the following relationship is one to one embodiment.

mysql> create table priv(
    -> id int auto_increment primary key,
    -> salary int not null default 0,
    -> uid int not null default 1,
    -> constraint fk_priv_user foreign key (uid) references userinfo(id),
    -> unique(uid)) charset=utf8;
Query OK, 0 rows affected (0.52 sec)

mysql> insert into priv(salary,uid) values (10000,1),(12000,2),(15000,3),(8000,4),(9000,5),(9900,6);
Query OK, 6 rows affected (0.16 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql> select name,salary from userinfo left join priv on priv.uid = userinfo.id;
+---------+--------+
| name    | salary |
+---------+--------+
| xiaozhu |  10000 |
| xiaoyu  |  12000 |
| laohe   |  15000 |
| longge  |   8000 |
| ludi    |   9000 |
| xiaoguo |   9900 |
+---------+--------+
6 rows in set (0.00 sec)

Over 3.4 meter joint investigation

Multi-table joint investigation is multiple related tables together on the investigation, the statement used are:

left join ...... on the query to the data of the left main

right join ...... on the query to the right of the main data

mysql> insert into department(name) values('业务部');
Query OK, 1 row affected (0.12 sec)

mysql> select userinfo.name as uname,department.name as dname from userinfo left
    -> join department on depart_id = department.id;
+---------+-----------+
| uname   | dname     |
+---------+-----------+
| xiaozhu | 研发部    |
| xiaoyu  | 研发部    |
| laohe   | 运维部    |
| longge  | 运维部    |
| ludi    | 前台部    |
| xiaoguo | 小卖部    |
+---------+-----------+
6 rows in set (0.00 sec)

mysql> select userinfo.name as uname,department.name as dname from userinfo right
    -> join department on depart_id = department.id;
+---------+-----------+
| uname   | dname     |
+---------+-----------+
| xiaozhu | 研发部    |
| xiaoyu  | 研发部    |
| laohe   | 运维部    |
| longge  | 运维部    |
| ludi    | 前台部    |
| xiaoguo | 小卖部    |
| NULL    | 业务部    |
+---------+-----------+
7 rows in set (0.00 sec)

Guess you like

Origin www.cnblogs.com/ghylpb/p/11767326.html