标签:not table cross sts order ros val employee any
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> SELECT*from department CROSS JOIN employee;
+-----+--------+----+------+-----+-----+
| did | dname | id | name | age | did |
+-----+--------+----+------+-----+-----+
| 1 | 网络部 | 1 | 王红 | 20 | 1 |
| 2 | 媒体部 | 1 | 王红 | 20 | 1 |
| 3 | 研发部 | 1 | 王红 | 20 | 1 |
| 4 | 人事部 | 1 | 王红 | 20 | 1 |
| 1 | 网络部 | 2 | 李强 | 22 | 1 |
| 2 | 媒体部 | 2 | 李强 | 22 | 1 |
| 3 | 研发部 | 2 | 李强 | 22 | 1 |
| 4 | 人事部 | 2 | 李强 | 22 | 1 |
| 1 | 网络部 | 3 | 赵四 | 20 | 2 |
| 2 | 媒体部 | 3 | 赵四 | 20 | 2 |
| 3 | 研发部 | 3 | 赵四 | 20 | 2 |
| 4 | 人事部 | 3 | 赵四 | 20 | 2 |
| 1 | 网络部 | 4 | 郝娟 | 20 | 4 |
| 2 | 媒体部 | 4 | 郝娟 | 20 | 4 |
| 3 | 研发部 | 4 | 郝娟 | 20 | 4 |
| 4 | 人事部 | 4 | 郝娟 | 20 | 4 |
+-----+--------+----+------+-----+-----+
16 rows in set
mysql> SELECT employee.name,department.dname FROM department JOIN employee ON department.did=employee.did;
+------+--------+
| name | dname |
+------+--------+
| 王红 | 网络部 |
| 李强 | 网络部 |
| 赵四 | 媒体部 |
| 郝娟 | 人事部 |
+------+--------+
4 rows in set
mysql> SELECT employee.id,employee.name,department.dname FROM department JOIN employee ON department.did=employee.did;
+----+------+--------+
| id | name | dname |
+----+------+--------+
| 1 | 王红 | 网络部 |
| 2 | 李强 | 网络部 |
| 3 | 赵四 | 媒体部 |
| 4 | 郝娟 | 人事部 |
+----+------+--------+
4 rows in set
mysql> SELECT employee.name,department.dname FROM department,employee WHERE department.did=employee.did;
+------+--------+
| name | dname |
+------+--------+
| 王红 | 网络部 |
| 李强 | 网络部 |
| 赵四 | 媒体部 |
| 郝娟 | 人事部 |
+------+--------+
4 rows in set
mysql> SELECT department.did,department.dname,employee.name FROM department LEFT JOIN employee ON department.did=employee.did;
+-----+--------+------+
| did | dname | name |
+-----+--------+------+
| 1 | 网络部 | 王红 |
| 1 | 网络部 | 李强 |
| 2 | 媒体部 | 赵四 |
| 3 | 研发部 | NULL |
| 4 | 人事部 | 郝娟 |
+-----+--------+------+
5 rows in set
mysql> SELECT*FROM department;
+-----+--------+
| did | dname |
+-----+--------+
| 1 | 网络部 |
| 2 | 媒体部 |
| 3 | 研发部 |
| 4 | 人事部 |
+-----+--------+
4 rows in 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> SELECT department.did,department.dname,employee.name FROM department RIGHT JOIN employee ON department.did=employee.did;
+------+--------+------+
| did | dname | name |
+------+--------+------+
| 1 | 网络部 | 王红 |
| 1 | 网络部 | 李强 |
| 2 | 媒体部 | 赵四 |
| NULL | NULL | 郝娟 |
+------+--------+------+
4 rows in set
mysql> SELECT employee.name,employee.age,department.dname FROM department JOIN employee ON department.did=employee.did order by age;
+------+-----+--------+
| name | age | dname |
+------+-----+--------+
| 王红 | 20 | 网络部 |
| 赵四 | 20 | 媒体部 |
| 李强 | 22 | 网络部 |
+------+-----+--------+
3 rows in 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> SELECT*FROM department WHERE EXISTS(select did from employee where age>21);
+-----+--------+
| did | dname |
+-----+--------+
| 1 | 网络部 |
| 2 | 媒体部 |
| 3 | 研发部 |
| 5 | 人事部 |
+-----+--------+
4 rows in set
mysql> SELECT did,dname FROM department WHERE did>ANY(SELECT did FROM employee);
+-----+--------+
| did | dname |
+-----+--------+
| 2 | 媒体部 |
| 3 | 研发部 |
| 5 | 人事部 |
+-----+--------+
3 rows in set
mysql> SELECT did,dname FROM department WHERE did>ANY(SELECT did FROM employee);
+-----+--------+
| did | dname |
+-----+--------+
| 2 | 媒体部 |
| 3 | 研发部 |
| 5 | 人事部 |
+-----+--------+
3 rows in 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
标签:not table cross sts order ros val employee any
原文地址:https://www.cnblogs.com/xcj890/p/12003890.html