MySQL delete語句


mysql> use course;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from students;
+-----+----------+--------+---------+--------+
| sid | sname    | gender | dept_id | sname2 |
+-----+----------+--------+---------+--------+
|   1 | aa       | 3      |       1 | NULL   |
|   4 | cc       | 3      |       1 | NULL   |
|   5 | NULL     | 1      |       1 | NULL   |
| 101 | zhangsan | male   |       1 | NULL   |
+-----+----------+--------+---------+--------+
4 rows in set (0.00 sec)

mysql> select * from students where sid=5;
+-----+-------+--------+---------+--------+
| sid | sname | gender | dept_id | sname2 |
+-----+-------+--------+---------+--------+
|   5 | NULL  | 1      |       1 | NULL   |
+-----+-------+--------+---------+--------+
1 row in set (0.00 sec)

mysql> select * from students order by sid;
+-----+----------+--------+---------+--------+
| sid | sname    | gender | dept_id | sname2 |
+-----+----------+--------+---------+--------+
|   1 | aa       | 3      |       1 | NULL   |
|   4 | cc       | 3      |       1 | NULL   |
|   5 | NULL     | 1      |       1 | NULL   |
| 101 | zhangsan | male   |       1 | NULL   |
+-----+----------+--------+---------+--------+
4 rows in set (0.01 sec)

mysql> select * from students order by sid desc;
+-----+----------+--------+---------+--------+
| sid | sname    | gender | dept_id | sname2 |
+-----+----------+--------+---------+--------+
| 101 | zhangsan | male   |       1 | NULL   |
|   5 | NULL     | 1      |       1 | NULL   |
|   4 | cc       | 3      |       1 | NULL   |
|   1 | aa       | 3      |       1 | NULL   |
+-----+----------+--------+---------+--------+
4 rows in set (0.00 sec)

mysql> select * from students order by sid desc limit 2;
+-----+----------+--------+---------+--------+
| sid | sname    | gender | dept_id | sname2 |
+-----+----------+--------+---------+--------+
| 101 | zhangsan | male   |       1 | NULL   |
|   5 | NULL     | 1      |       1 | NULL   |
+-----+----------+--------+---------+--------+
2 rows in set (0.00 sec)

mysql> delete from students order by sid desc limit 2;
Query OK, 2 rows affected (0.13 sec)

mysql> select * from students;
+-----+-------+--------+---------+--------+
| sid | sname | gender | dept_id | sname2 |
+-----+-------+--------+---------+--------+
|   1 | aa    | 3      |       1 | NULL   |
|   4 | cc    | 3      |       1 | NULL   |
+-----+-------+--------+---------+--------+
2 rows in set (0.00 sec)
mysql> select * from students3;
Empty set (0.00 sec)

mysql> insert into students3 values(100,null,1,1,null);
Query OK, 1 row affected (0.03 sec)

mysql> select * from students2;
+-----+----------+--------+---------+
| sid | sname    | gender | dept_id |
+-----+----------+--------+---------+
| 100 | zhangsan | male   |       1 |
| 101 | zhangsan | male   |      10 |
+-----+----------+--------+---------+
2 rows in set (0.00 sec)

mysql> select * from students3;
+-----+-------+--------+---------+--------+
| sid | sname | gender | dept_id | sname2 |
+-----+-------+--------+---------+--------+
| 100 | NULL  | 1      |       1 | NULL   |
+-----+-------+--------+---------+--------+
1 row in set (0.00 sec)

mysql> delete students2,students3 from students2 inner join students3
    -> where students2.sid=students3.sid;
Query OK, 2 rows affected (0.05 sec)

mysql> select * from students2;
+-----+----------+--------+---------+
| sid | sname    | gender | dept_id |
+-----+----------+--------+---------+
| 101 | zhangsan | male   |      10 |
+-----+----------+--------+---------+
1 row in set (0.00 sec)

mysql> select * from students3;
Empty set (0.00 sec)

mysql> insert into students3 values(100,null,1,1,null);
Query OK, 1 row affected (0.03 sec)

mysql> select * from students3;
+-----+-------+--------+---------+--------+
| sid | sname | gender | dept_id | sname2 |
+-----+-------+--------+---------+--------+
| 100 | NULL  | 1      |       1 | NULL   |
+-----+-------+--------+---------+--------+
1 row in set (0.00 sec)

mysql> insert into students2 values(100,'a',1,1);
Query OK, 1 row affected (0.08 sec)

mysql> select * from students2;
+-----+----------+--------+---------+
| sid | sname    | gender | dept_id |
+-----+----------+--------+---------+
| 101 | zhangsan | male   |      10 |
| 100 | a        | 1      |       1 |
+-----+----------+--------+---------+
2 rows in set (0.00 sec)

mysql> select * from students3;
+-----+-------+--------+---------+--------+
| sid | sname | gender | dept_id | sname2 |
+-----+-------+--------+---------+--------+
| 100 | NULL  | 1      |       1 | NULL   |
+-----+-------+--------+---------+--------+
1 row in set (0.00 sec)

mysql> delete students2 from students2 inner join students3
    -> where students2.sid=students3.sid;
Query OK, 1 row affected (0.06 sec)

mysql> select * from students2;
+-----+----------+--------+---------+
| sid | sname    | gender | dept_id |
+-----+----------+--------+---------+
| 101 | zhangsan | male   |      10 |
+-----+----------+--------+---------+
1 row in set (0.00 sec)

mysql> select * from students3;
+-----+-------+--------+---------+--------+
| sid | sname | gender | dept_id | sname2 |
+-----+-------+--------+---------+--------+
| 100 | NULL  | 1      |       1 | NULL   |
+-----+-------+--------+---------+--------+
1 row in set (0.00 sec)

mysql> select * from students2 inner join students3 where students2.sid=students3.sid;
Empty set (0.00 sec)
mysql> delete a from students2 as a inner join students3 as b where a.sid=b.sid;
Query OK, 0 rows affected (0.00 sec)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM