Mysql Join- join queries (on)

understanding

Multi-table join queries , I feel it should be local relational database that best reflect its value and flexibility of it. I think the role of the database, summed up in nothing more than storage and query .

In a nutshell, the database is the flexibility to store and query data on storage, but also the way the document is stored (Linux point of view, everything is a file), the file is stored in the data carrier, the database file is with other data file difference it is regular . there are constraints and norms, unlike the work as many places in messing around, it is because there is prior agreement , the database retrieves data very fast.

Of course, the storage, I usually use the words on their own stand-alone deposit to the company's words are generally distributed storage , the data are actually distributed on each machine, what master-slave configuration and the like. I usually learn by for storage fell less attention, of course, will practice under a master-slave configuration.

Obviously most of our department up to now have not really spend time with the database query, the book between every system has some small problems, the daily work is to export the query sets from various systems, Excel performed manually after Vlookup make pure manual the number is going to be the main reason for the departure. after all, vlookup + on the efficiency sumifs / contifs / pivote table is far less than the select sql, join, aggregation of , but both the work done is the same, but efficiency difference.

Table commonly used even left (outer) connector, a right (outer) connector, the connector, Cartesian product, etc. usually use vlookup actually left connection . Can be connected between the table and the table, there must be the basis for connection, That is a common field (Key) , or else spliced directly, rather than connected.

  • inner join: two tables "common ground" that the left table and right table through the common bond linking mode, a total recording
  • left join: is vlookup, records show all the left table, right table displays only records on the "match", where there is no matching the right table on the left table is shown as Null
  • right join: with the left is the same, just right perspective

I this year's business, are vlookup left connected, the basic need inner join, why, requirements of the business, is to do basic table to the left of the number, or continue to match data from other tables (right), lead difficult to program procedures. here still in front of student, classes table presentation.

Simple join query (inner, left, right)

-- 登录mysql客户端, 查看下使用的数据集
mysql> use student_db;
Database changed
mysql> show tables;
+----------------------+
| Tables_in_student_db |
+----------------------+
| classes              |
| clock_in             |
| students             |
+----------------------+
3 rows in set (0.04 sec)

mysql> select * from classes;
+----+------+
| id | name |
+----+------+
|  1 | 科学 |
|  2 | 艺术 |
+----+------+
2 rows in set (0.09 sec)

mysql> select * from students;
+----+----------+-----+--------+--------+----------+-----------+
| id | name     | age | height | gender | class_id | is_delete |
+----+----------+-----+--------+--------+----------+-----------+
|  1 | 爱因斯坦 |  18 | 180.00 | 男     |        1 | 0         |
|  2 | 居里夫人 |  18 | 180.00 | 女     |        2 | 1         |
|  3 | 小王子   |  14 | 185.00 | 男     |        1 | 0         |
|  4 | 李银河   |  59 | 175.00 | 男     |        2 | 1         |
|  5 | 黄蓉     |  38 | 160.00 | 女     |        1 | 0         |
|  6 | 冰心     |  28 | 150.00 | 女     |        2 | 1         |
|  7 | 王祖贤   |  18 | 172.00 | 女     |        1 | 1         |
|  8 | 周杰伦   |  36 | NULL   | 男     |        1 | 0         |
|  9 | 王小波   |  57 | 181.00 | 男     |        2 | 0         |
| 10 | 林徽因   |  25 | 166.00 | 女     |        2 | 0         |
| 11 | 小星     |  33 | 162.00 | 未填写 |        3 | 1         |
| 12 | 张爱玲   |  12 | 180.00 | 女     |        4 | 0         |
| 13 | 冯唐     |  12 | 170.00 | 男     |        4 | 0         |
| 14 | 胡适     |  34 | 176.00 | 男     |        5 | 0         |
+----+----------+-----+--------+--------+----------+-----------+
14 rows in set (0.25 sec)

If there is no test data is their create database / table, and then create table .. and then insert into tb_name values ​​(), (), ... Yeah.

Related basis: id students and classes class_id table table indicates the same thing (class id)

Supplementary write the sql skills (running order of approximately computer simulation to)

  • To write from tb_name1 as a, tb_name2 as b (alias)
  • Then the connection condition join on .... (spend alias)
  • Then write where ......
  • Finally, write select ....

select ended up writing, inquiry into blocks, and then splice (join, union ...), there are moments my mind the image of the table .

-- 检索出学生和班级能对应上的信息
select s.*, c.*
from students s, classes c  -- as可省略
where s.class_id = c.id;

-- 这种默认的where等值连接, 其实就跟 inner join 是一样的
-- out
+----+----------+-----+--------+--------+----------+-----------+----+------+
| id | name     | age | height | gender | class_id | is_delete | id | name |
+----+----------+-----+--------+--------+----------+-----------+----+------+
|  1 | 爱因斯坦 |  18 | 180.00 | 男     |        1 | 0         |  1 | 科学 |
|  2 | 居里夫人 |  18 | 180.00 | 女     |        2 | 1         |  2 | 艺术 |
|  3 | 小王子   |  14 | 185.00 | 男     |        1 | 0         |  1 | 科学 |
|  4 | 李银河   |  59 | 175.00 | 男     |        2 | 1         |  2 | 艺术 |
|  5 | 黄蓉     |  38 | 160.00 | 女     |        1 | 0         |  1 | 科学 |
|  6 | 冰心     |  28 | 150.00 | 女     |        2 | 1         |  2 | 艺术 |
|  7 | 王祖贤   |  18 | 172.00 | 女     |        1 | 1         |  1 | 科学 |
|  8 | 周杰伦   |  36 | NULL   | 男     |        1 | 0         |  1 | 科学 |
|  9 | 王小波   |  57 | 181.00 | 男     |        2 | 0         |  2 | 艺术 |
| 10 | 林徽因   |  25 | 166.00 | 女     |        2 | 0         |  2 | 艺术 |
+----+----------+-----+--------+--------+----------+-----------+----+------+

-- inner join on 
select s.*, c.*
from students s
inner join classes c
on s.class_id = c.id;

-- out
+----+----------+-----+--------+--------+----------+-----------+----+------+
| id | name     | age | height | gender | class_id | is_delete | id | name |
+----+----------+-----+--------+--------+----------+-----------+----+------+
|  1 | 爱因斯坦 |  18 | 180.00 | 男     |        1 | 0         |  1 | 科学 |
|  2 | 居里夫人 |  18 | 180.00 | 女     |        2 | 1         |  2 | 艺术 |
|  3 | 小王子   |  14 | 185.00 | 男     |        1 | 0         |  1 | 科学 |
|  4 | 李银河   |  59 | 175.00 | 男     |        2 | 1         |  2 | 艺术 |
|  5 | 黄蓉     |  38 | 160.00 | 女     |        1 | 0         |  1 | 科学 |
|  6 | 冰心     |  28 | 150.00 | 女     |        2 | 1         |  2 | 艺术 |
|  7 | 王祖贤   |  18 | 172.00 | 女     |        1 | 1         |  1 | 科学 |
|  8 | 周杰伦   |  36 | NULL   | 男     |        1 | 0         |  1 | 科学 |
|  9 | 王小波   |  57 | 181.00 | 男     |        2 | 0         |  2 | 艺术 |
| 10 | 林徽因   |  25 | 166.00 | 女     |        2 | 0         |  2 | 艺术 |
+----+----------+-----+--------+--------+----------+-----------+----+------+

To the table, as the field aliased

-- 查询前3个艺术班女生的姓名和班级
select s.name as "姓名", c.name as "班级"
from students s
inner join classes c
on s.class_id = c.id
where (s.gender = "女") and (c.name = "艺术")
limit 3;

-- out
+----------+------+
| 姓名     | 班级 |
+----------+------+
| 居里夫人 | 艺术 |
| 冰心     | 艺术 |
| 林徽因   | 艺术 |
+----------+------+
3 rows in set (0.15 sec)

Not difficult to find, in fact, sql is very simple, as long as your logic is clear, check out little by little. In fact, it is nothing more than two forms, multi-table joins + where + ... or with sub-queries and then to of Union , the routine is the same.

-- 查询能对应上班级的学生和班级信息, 并按照班级名称降序
select * 
from students s, classes c
where s.class_id = c.id
order by c.name desc
limit 3;

-- out
+----+--------+-----+--------+--------+----------+-----------+----+------+
| id | name   | age | height | gender | class_id | is_delete | id | name |
+----+--------+-----+--------+--------+----------+-----------+----+------+
|  4 | 李银河 |  59 | 175.00 | 男     |        2 | 1         |  2 | 艺术 |
|  6 | 冰心   |  28 | 150.00 | 女     |        2 | 1         |  2 | 艺术 |
| 10 | 林徽因 |  25 | 166.00 | 女     |        2 | 0         |  2 | 艺术 |
+----+--------+-----+--------+--------+----------+-----------+----+------+
3 rows in set (0.07 sec)

-- 先按班级名降序, 如果班级名相同, 则按身高降序, 再则按学生id升序
select s.name, s.height, c.name
from students s, classes c
where s.class_id = c.id
order by c.name desc, s.height desc, s.id asc
limit 6;

-- out
+----------+--------+------+
| name     | height | name |
+----------+--------+------+
| 王小波   | 181.00 | 艺术 |
| 居里夫人 | 180.00 | 艺术 |
| 李银河   | 175.00 | 艺术 |
| 林徽因   | 166.00 | 艺术 |
| 冰心     | 150.00 | 艺术 |
| 小王子   | 185.00 | 科学 |
+----------+--------+------+
6 rows in set (0.09 sec)

I left join in a day at work, with vlookup Excel function of the same, are to the left table are matched based on the display as Null did not match

-- 从classes表中, 匹配出student的姓名和班级
select s.name as "姓名", c.name as "班级"
from students s 
left join classes c
on s.class_id = c.id;

-- out
+----------+------+
| 姓名     | 班级 |
+----------+------+
| 爱因斯坦 | 科学 |
| 小王子   | 科学 |
| 黄蓉     | 科学 |
| 王祖贤   | 科学 |
| 周杰伦   | 科学 |
| 居里夫人 | 艺术 |
| 李银河   | 艺术 |
| 冰心     | 艺术 |
| 王小波   | 艺术 |
| 林徽因   | 艺术 |
| 小星     | NULL |
| 张爱玲   | NULL |
| 冯唐     | NULL |
| 胡适     | NULL |
+----------+------+
14 rows in set (0.18 sec)

-- 查询出没有对应班级的学生姓名
select s.name, c.name
from students s
left join classes c
on s.class_id = c.id
having c.name is null;

+--------+------+
| name   | name |
+--------+------+
| 小星   | NULL |
| 张爱玲 | NULL |
| 冯唐   | NULL |
| 胡适   | NULL |
+--------+------+
4 rows in set (0.05 sec)

-- 过滤条件用where也是可以的
select s.name, c.name 
from students s 
left join classes c 
on s.class_id = c.id
where c.name is null;

And having a difference where

  • where the data is filtered before packet, aggregate functions can not be used behind where
  • hvaing are grouped filter the data, having the function can be followed by polymerization of
  • Query execution order: from> WHERE> Group and by aggregation function> having> order> select

  • having i.e. where insufficient to make up the group by when, as where> aggregate functions

Subqueries

The so-called sub-query is unable to select a nested Yeah, as long as there is this little learned a little programming partners are immediately able to get, even if not programming, I look at my colleagues formulate nesting Excel is also very powerful, the outer layer of a sumifs inside a vlookup .... anyway principles are the same.

  • Scalar query: a query result value
  • Liezi Query: The result is a column returned
  • Row subquery: the result is a return line
-- 标量: 查询出高于平均身高的学生姓名, 身高, 所在班级

-- step 1: 先查出身高均值(标量)
select avg(height) from students;
+-------------+
| avg(height) |
+-------------+
| 172.076923  |
+-------------+
1 row in set (0.07 sec)

-- step2: 嵌套进来呀
select s.name, s.height, c.name
from students s
left join classes c
on class_id = c.id
where s.height > (select avg(height) from students);

+----------+--------+------+
| name     | height | name |
+----------+--------+------+
| 爱因斯坦 | 180.00 | 科学 |
| 小王子   | 185.00 | 科学 |
| 居里夫人 | 180.00 | 艺术 |
| 李银河   | 175.00 | 艺术 |
| 王小波   | 181.00 | 艺术 |
| 张爱玲   | 180.00 | NULL |
| 胡适     | 176.00 | NULL |
+----------+--------+------+
7 rows in set (0.11 sec)

Liezi inquiry: namely a Machado , keyword: in

-- 查询出, 根据班级id, 所对应的学生名字

-- 分析,一个班级ID -> 多个名字, 是一查多(列子查询)
select s.name
from students s
where s.class_id in (select id from classes)

-- out
| name    |
+----------+
| 爱因斯坦 |
| 居里夫人 |
| 小王子   |
| 李银河   |
| 黄蓉     |
| 冰心     |
| 王祖贤   |
| 周杰伦   |
| 王小波   |
| 林徽因   |
+----------+

Subquery row (row a plurality of element fields synthetic)

-- 查询出 年龄最大, 且身高最高的的学生信息
select * 
from students s
where (s.age, s.height) = (select max(age), max(height) from students);

-- out: 可能不存在这样牛逼的记录
Empty set

summary

  • Two core database functions to store and retrieve data

  • Queries efficient because, organize data files, storage type is a regular convention .
  • Execution order is from> where> group by and aggregation functions> having> order> select

  • Common table connected to the inner join, left join, right join ... I usually use most of the left join because Excel's vlookup reasons
  • Sub-query is actually nested sql, it is based on the results returned by the scalar, column, row form

The talk took a self-join, advanced multi-table joint inquiry, that subquery connected with the table, where, union these ....

Guess you like

Origin www.cnblogs.com/chenjieyouge/p/11789332.html
Recommended