Operatively associated methodology


mysql> CREATE DATABASE student_tb;
Query OK, 1 row affected

mysql> USE student_tb;
Database changed


mysql> CREATE TABLE department(
-> did int(4) NOT NULL PRIMARY KEY,
-> dname varchar(36)
-> );
Query OK, 0 rows affected

mysql> CREATE TABLE employee(
-> id int(4) NOT NULL PRIMARY KEY,
-> name varchar(36),
-> age int(2),
-> did int(4) NOT NULL
-> );
Query OK, 0 rows affected

mysql> INSERT INTO department(did,dname)VALUES(1,'网络部');
Query OK, 1 row affected

mysql> INSERT INTO department(did,dname)VALUES(2,'媒体部');
Query OK, 1 row affected

mysql> INSERT INTO department(did,dname)VALUES(3,'研发部');
Query OK, 1 row affected

mysql> INSERT INTO department(did,dname)VALUES(4,'人事部');
Query OK, 1 row affected

mysql> INSERT INTO employee(id,name,age,did)VALUES(1,'王红',20,1);
Query OK, 1 row affected

mysql> INSERT INTO employee(id,name,age,did)VALUES(2,'李强',22,1);
Query OK, 1 row affected

mysql> INSERT INTO employee(id,name,age,did)VALUES(3,'赵四',20,2);
Query OK, 1 row affected

mysql> INSERT INTO employee(id,name,age,did)VALUES(4,'郝娟',20,4);
Query OK, 1 row affected

MySQL> the SELECT * from the CROSS the JOIN Department Employee;
+ -------- + ----- + ---- + ------ + ----- + ----- +
| DID | DNAME | the above mentioned id | name | Age | DID |
+ ----- + -------- + ---- + ------ + ----- + --- - +
| 1 | networks | 1 | WANG | 20 is | 1 |
| 2 | media portions | 1 | WANG | 20 is | 1 |
|. 3 | R & D | 1 | WANG | 20 is | 1 |
|. 4 | personnel Department | 1 | WANG | 20 | 1 |
| 1 | networks | 2 | Li Qiang | 22 | 1 |
| 2 | media section | 2 | Li Qiang | 22 | 1 |
| 3 | R & D | 2 | Li Qiang | 22 is |. 1 |
|. 4 | personnel Department | 2 | Qiang | 22 is |. 1 |
|. 1 | networks | 3 | Zhao Si | 20 is | 2 |
| 2 | media section | 3 | Zhao Si | 20 is | 2 |
| 3 | R & D | 3 | Zhao Si | 20 is | 2 |
| 4 | personnel Department | 3 | Zhao Si | 20 is | 2 |
|. 1 | networks | 4 | HAO Juan | 20 is | 4 |
| 2 | media portions | 4 | Hao Juan | 20 | 4 |
| 3 | R & D | 4 | Hao Juan | 20 | 4 |
| 4 | Personnel Department | 4 | Hao Juan | 20 | 4 |
+ ----- + -------- + ---- + ------ + ----- + - + ----
16 rows in the SET

MySQL> employee.name the SELECT, the JOIN department.dname the FROM Employee Department department.did the ON = employee.did;
+ ------ + -------- +
| name | DNAME |
+ ---- - + -------- +
| WANG | networks |
| Li Qiang | networks |
| Zhao Si | media section |
| Hao Juan | personnel Department |
+ ------ + --- + -----
4 rows in the SET

MySQL> employee.id the SELECT, employee.name, the JOIN department.dname the FROM Employee Department department.did the ON = employee.did;
+ ---- + ------ + -------- +
| ID | name | DNAME |
+ ---- + ------ + -------- +
|. 1 | WANG | networks |
| 2 | Qiang | networks |
|. 3 | Zhao Si | media section |
| 4 | Hao Juan | personnel Department |
+ ---- + ------ + -------- +
4 rows in the SET

MySQL> the SELECT employee.name, department.dname the FROM Department, the Employee the WHERE department.did = employee.did;
+ ------ + -------- +
| name | DNAME |
+ ---- - + -------- +
| WANG | networks |
| Li Qiang | networks |
| Zhao Si | media section |
| Hao Juan | personnel Department |
+ ------ + --- + -----
4 rows in the SET

MySQL> department.did the SELECT, department.dname, the LEFT Department employee.name the FROM Employee the ON department.did the JOIN = employee.did;
+ -------- + ------ + ----- +
| DID | DNAME | name |
+ -------- + ------ + ----- +
|. 1 | networks | WANG |
|. 1 | networks | Qiang |
| 2 | media section | Zhao Si |
| 3 | R & D | NULL |
| 4 | personnel Department | Hao Juan |
+ ----- + -------- + ------ +
5 rows in set

MySQL> the SELECT * the FROM Department;
+ ----- + -------- +
| DID | DNAME |
+ ----- + -------- +
|. 1 | Networks |
| 2 | media section |
| 3 | R & D |
| 4 | personnel Department |
+ ----- + -------- +
4 rows in the SET

mysql> UPDATE department SET did=5 WHERE dname='人事部';

Query OK, 1 row affected
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT SELECT*FROM department;
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT*FROM department' at line 1
mysql> SELECT*FROM department;
+-----+--------+
| did | dname |
+-----+--------+
| 1 | 网络部 |
| 2 | 媒体部 |
| 3 | 研发部 |
| 5 | 人事部 |
+-----+--------+
4 rows in set

MySQL> department.did the SELECT, department.dname, Department employee.name the FROM Employee the ON RIGHT the JOIN department.did = employee.did;
+ ------ + ----- + -------- - +
| DID | DNAME | name |
+ ------ + -------- + ------ +
| 1 | networks | WANG |
| 1 | networks | Li Qiang |
| 2 | media portions | Zhao Si |
| NULL | NULL | HAO Juan |
+ -------- + ------ + ------ +
. 4 in rows SET

MySQL> employee.name the SELECT, employee.age, the JOIN department.dname the FROM Employee Department employee.did Order by the ON department.did = Age;
+ ------ + ------ + ----- - +
| name | Age | DNAME |
+ ------ + ----- + -------- +
| WANG | 20 | networks |
| Zhao Si | 20 | media Department |
| Li Qiang | 22 | networks |
+ ------ + ----- + -------- +
3 rows in the SET

mysql> SELECT did FROM employee WHERE age=20;
+-----+
| did |
+-----+
| 1 |
| 2 |
| 4 |
+-----+
3 rows in set

mysql> SELECT did,dname FROM department WHERE did IN(1,2,4);
+-----+--------+
| did | dname |
+-----+--------+
| 1 | 网络部 |
| 2 | 媒体部 |
+-----+--------+
2 rows in set

mysql> SELECT did,dname FROM department WHERE did IN(SELECT did FROM employee WHERE age=20);
+-----+--------+
| did | dname |
+-----+--------+
| 1 | 网络部 |
| 2 | 媒体部 |
+-----+--------+
2 rows in set

mysql> SELECT did,dname FROM department WHERE did NOT IN(SELECT did FROM employee WHERE age=20);
+-----+--------+
| did | dname |
+-----+--------+
| 3 | 研发部 |
| 5 | 人事部 |
+-----+--------+
2 rows in set

MySQL> the SELECT * the FROM Department the WHERE EXISTS (the SELECT from the Employee the WHERE DID Age> 21);
+ ----- + -------- +
| DID | DNAME |
+ ----- + --- + -----
| 1 | networks |
| 2 | media section |
| 3 | R & D |
| 5 | personnel Department |
+ ----- + -------- +
4 rows in the SET

MySQL> the SELECT DID, the FROM Department DNAME the WHERE DID> the ANY (the SELECT DID the FROM the Employee);
+ ----- + -------- +
| DID | DNAME |
+ ----- + --- + -----
| 2 | media section |
| 3 | R & D |
| 5 | personnel Department |
+ ----- + -------- +
3 rows in the SET

MySQL> the SELECT DID, the FROM Department DNAME the WHERE DID> the ANY (the SELECT DID the FROM the Employee);
+ ----- + -------- +
| DID | DNAME |
+ ----- + --- + -----
| 2 | media section |
| 3 | R & D |
| 5 | personnel Department |
+ ----- + -------- +
3 rows in the SET

mysql> SELECT did FROM employee WHERE name="赵四";
+-----+
| did |
+-----+
| 2 |
+-----+
1 row in set

mysql> SELECT* FROM department WHERE did=2;
+-----+--------+
| did | dname |
+-----+--------+
| 2 | 媒体部 |
+-----+--------+
1 row in set

Guess you like

Origin www.cnblogs.com/xcj890/p/12003890.html
Recommended