• 【一】【SQL】表的增删查改(部分)


    表之“增”操作

    建表的操作

     
    
    1. mysql> create table students(
    2. -> id int unsigned primary key auto_increment,
    3. -> sn int unsigned unique key,
    4. -> name varchar(20) not null,
    5. -> qq varchar(32) unique key
    6. -> );
    7. Query OK, 0 rows affected (0.03 sec)
    8. mysql> desc students;
    9. +-------+------------------+------+-----+---------+----------------+
    10. | Field | Type | Null | Key | Default | Extra |
    11. +-------+------------------+------+-----+---------+----------------+
    12. | id | int(10) unsigned | NO | PRI | NULL | auto_increment |
    13. | sn | int(10) unsigned | YES | UNI | NULL | |
    14. | name | varchar(20) | NO | | NULL | |
    15. | qq | varchar(32) | YES | UNI | NULL | |
    16. +-------+------------------+------+-----+---------+----------------+
    17. 4 rows in set (0.01 sec)

    指定列插入

     
    
    1. mysql> insert into students (sn,name,qq) values (123,'张飞','4567890');
    2. Query OK, 1 row affected (0.00 sec)
    3. mysql> select * from students;
    4. +----+------+--------+---------+
    5. | id | sn | name | qq |
    6. +----+------+--------+---------+
    7. | 1 | 123 | 张飞 | 4567890 |
    8. +----+------+--------+---------+
    9. 1 row in set (0.00 sec)

    全列插入

     
    
    1. mysql> insert into students values (10,124,'关羽','123456');
    2. Query OK, 1 row affected (0.00 sec)
    3. mysql> select * from students;
    4. +----+------+--------+---------+
    5. | id | sn | name | qq |
    6. +----+------+--------+---------+
    7. | 1 | 123 | 张飞 | 4567890 |
    8. | 10 | 124 | 关羽 | 123456 |
    9. +----+------+--------+---------+
    10. 2 rows in set (0.00 sec)

    主键约束展示

     
    
    1. mysql> insert into students values (10,124,'关羽','123456');
    2. ERROR 1062 (23000): Duplicate entry '10' for key 'PRIMARY'
    3. mysql> insert into students values (11,125,'刘备','123459990');
    4. Query OK, 1 row affected (0.01 sec)
    5. mysql> select * from students;
    6. +----+------+--------+-----------+
    7. | id | sn | name | qq |
    8. +----+------+--------+-----------+
    9. | 1 | 123 | 张飞 | 4567890 |
    10. | 10 | 124 | 关羽 | 123456 |
    11. | 11 | 125 | 刘备 | 123459990 |
    12. +----+------+--------+-----------+
    13. 3 rows in set (0.00 sec)
    14. mysql>

    多列插入---全列

     
    
    1. mysql> insert into students values (13,127,'许攸','1234545656'),(14,128,'许褚','1123334455'),(15,129,'诸葛亮','32111234343');
    2. Query OK, 3 rows affected (0.00 sec)
    3. Records: 3 Duplicates: 0 Warnings: 0
    4. mysql> select * from students;
    5. +----+------+-----------+-------------+
    6. | id | sn | name | qq |
    7. +----+------+-----------+-------------+
    8. | 1 | 123 | 张飞 | 4567890 |
    9. | 10 | 124 | 关羽 | 123456 |
    10. | 11 | 125 | 刘备 | 123459990 |
    11. | 13 | 127 | 许攸 | 1234545656 |
    12. | 14 | 128 | 许褚 | 1123334455 |
    13. | 15 | 129 | 诸葛亮 | 32111234343 |
    14. +----+------+-----------+-------------+
    15. 6 rows in set (0.00 sec)
    16. mysql>

    多列插入---指定列

     
    
    1. mysql> insert into students (sn,name,qq) values (130,'孙权','64533764'),(131,'吕布','4232455');
    2. Query OK, 2 rows affected (0.01 sec)
    3. Records: 2 Duplicates: 0 Warnings: 0
    4. mysql> select * from students;
    5. +----+------+-----------+-------------+
    6. | id | sn | name | qq |
    7. +----+------+-----------+-------------+
    8. | 1 | 123 | 张飞 | 4567890 |
    9. | 10 | 124 | 关羽 | 123456 |
    10. | 11 | 125 | 刘备 | 123459990 |
    11. | 13 | 127 | 许攸 | 1234545656 |
    12. | 14 | 128 | 许褚 | 1123334455 |
    13. | 15 | 129 | 诸葛亮 | 32111234343 |
    14. | 16 | 130 | 孙权 | 64533764 |
    15. | 17 | 131 | 吕布 | 4232455 |
    16. +----+------+-----------+-------------+
    17. 8 rows in set (0.00 sec)
    18. mysql>

    表之“改”操作

    键冲突时修改数据(冲突键不变)

     
    
    1. mysql> select * from students;
    2. +----+------+-----------+-------------+
    3. | id | sn | name | qq |
    4. +----+------+-----------+-------------+
    5. | 1 | 123 | 张飞 | 4567890 |
    6. | 10 | 124 | 关羽 | 123456 |
    7. | 11 | 125 | 刘备 | 123459990 |
    8. | 13 | 127 | 许攸 | 1234545656 |
    9. | 14 | 128 | 许褚 | 1123334455 |
    10. | 15 | 129 | 诸葛亮 | 32111234343 |
    11. | 16 | 130 | 孙权 | 64533764 |
    12. | 17 | 131 | 吕布 | 4232455 |
    13. +----+------+-----------+-------------+
    14. 8 rows in set (0.00 sec)
    15. mysql> insert into students values (13,128,'xuyou','11111111');
    16. ERROR 1062 (23000): Duplicate entry '13' for key 'PRIMARY'
    17. mysql> insert into students values (13,132,'xuyou','11111111') on duplicate key update sn=132,name='xuyou',qq='11111111';
    18. Query OK, 2 rows affected (0.00 sec)
    19. mysql> select * from students;
    20. +----+------+-----------+-------------+
    21. | id | sn | name | qq |
    22. +----+------+-----------+-------------+
    23. | 1 | 123 | 张飞 | 4567890 |
    24. | 10 | 124 | 关羽 | 123456 |
    25. | 11 | 125 | 刘备 | 123459990 |
    26. | 13 | 132 | xuyou | 11111111 |
    27. | 14 | 128 | 许褚 | 1123334455 |
    28. | 15 | 129 | 诸葛亮 | 32111234343 |
    29. | 16 | 130 | 孙权 | 64533764 |
    30. | 17 | 131 | 吕布 | 4232455 |
    31. +----+------+-----------+-------------+
    32. 8 rows in set (0.00 sec)
    33. mysql> insert into students values (18,134,'貂蝉','111223234') on duplicate key update sn=134,name='貂蝉',qq='111223234';
    34. Query OK, 1 row affected (0.00 sec)
    35. mysql> insert into students values (18,134,'貂蝉','111223234') on duplicate key update sn=134,name='貂蝉',qq='111223234';
    36. Query OK, 0 rows affected (0.00 sec)
    37. mysql> select * from students;
    38. +----+------+-----------+-------------+
    39. | id | sn | name | qq |
    40. +----+------+-----------+-------------+
    41. | 1 | 123 | 张飞 | 4567890 |
    42. | 10 | 124 | 关羽 | 123456 |
    43. | 11 | 125 | 刘备 | 123459990 |
    44. | 13 | 132 | xuyou | 11111111 |
    45. | 14 | 128 | 许褚 | 1123334455 |
    46. | 15 | 129 | 诸葛亮 | 32111234343 |
    47. | 16 | 130 | 孙权 | 64533764 |
    48. | 17 | 131 | 吕布 | 4232455 |
    49. | 18 | 134 | 貂蝉 | 111223234 |
    50. +----+------+-----------+-------------+
    51. 9 rows in set (0.00 sec)
    52. mysql> insert into students values (18,134,'貂蝉','111223234') on duplicate key update sn=134,name='貂蝉',qq='1112235554';
    53. Query OK, 2 rows affected (0.00 sec)
    54. mysql> select * from students;
    55. +----+------+-----------+-------------+
    56. | id | sn | name | qq |
    57. +----+------+-----------+-------------+
    58. | 1 | 123 | 张飞 | 4567890 |
    59. | 10 | 124 | 关羽 | 123456 |
    60. | 11 | 125 | 刘备 | 123459990 |
    61. | 13 | 132 | xuyou | 11111111 |
    62. | 14 | 128 | 许褚 | 1123334455 |
    63. | 15 | 129 | 诸葛亮 | 32111234343 |
    64. | 16 | 130 | 孙权 | 64533764 |
    65. | 17 | 131 | 吕布 | 4232455 |
    66. | 18 | 134 | 貂蝉 | 1112235554 |
    67. +----+------+-----------+-------------+
    68. 9 rows in set (0.00 sec)
    69. mysql>

    -- 0 row affected :表中有冲突数据,但冲突数据的值和update的值相等

    -- 1 row affected:表中没有冲突数据,数据被插入

    -- 2 row affected:表中有冲突数据,并且数据已经被更新

    键冲突时修改数据(冲突键会变)

     
    
    1. mysql> select * from students;
    2. +----+------+-----------+-------------+
    3. | id | sn | name | qq |
    4. +----+------+-----------+-------------+
    5. | 1 | 123 | 张飞 | 4567890 |
    6. | 10 | 124 | 关羽 | 123456 |
    7. | 11 | 125 | 刘备 | 123459990 |
    8. | 13 | 132 | xuyou | 11111111 |
    9. | 14 | 128 | 许褚 | 1123334455 |
    10. | 15 | 129 | 诸葛亮 | 32111234343 |
    11. | 16 | 130 | 孙权 | 64533764 |
    12. | 17 | 131 | 吕布 | 4232455 |
    13. | 18 | 134 | 貂蝉 | 1112235554 |
    14. +----+------+-----------+-------------+
    15. 9 rows in set (0.00 sec)
    16. mysql> desc students;
    17. +-------+------------------+------+-----+---------+----------------+
    18. | Field | Type | Null | Key | Default | Extra |
    19. +-------+------------------+------+-----+---------+----------------+
    20. | id | int(10) unsigned | NO | PRI | NULL | auto_increment |
    21. | sn | int(10) unsigned | YES | UNI | NULL | |
    22. | name | varchar(20) | NO | | NULL | |
    23. | qq | varchar(32) | YES | UNI | NULL | |
    24. +-------+------------------+------+-----+---------+----------------+
    25. 4 rows in set (0.00 sec)
    26. mysql> replace into students (sn,name,qq)values(140,'许攸','31213554');
    27. Query OK, 1 row affected (0.00 sec)
    28. mysql> select * from students;
    29. +----+------+-----------+-------------+
    30. | id | sn | name | qq |
    31. +----+------+-----------+-------------+
    32. | 1 | 123 | 张飞 | 4567890 |
    33. | 10 | 124 | 关羽 | 123456 |
    34. | 11 | 125 | 刘备 | 123459990 |
    35. | 13 | 132 | xuyou | 11111111 |
    36. | 14 | 128 | 许褚 | 1123334455 |
    37. | 15 | 129 | 诸葛亮 | 32111234343 |
    38. | 16 | 130 | 孙权 | 64533764 |
    39. | 17 | 131 | 吕布 | 4232455 |
    40. | 18 | 134 | 貂蝉 | 1112235554 |
    41. | 19 | 140 | 许攸 | 31213554 |
    42. +----+------+-----------+-------------+
    43. 10 rows in set (0.00 sec)
    44. mysql> replace into students (sn,name,qq)values(140,'许攸','31213554');
    45. Query OK, 2 rows affected (0.00 sec)
    46. mysql> select * from students;
    47. +----+------+-----------+-------------+
    48. | id | sn | name | qq |
    49. +----+------+-----------+-------------+
    50. | 1 | 123 | 张飞 | 4567890 |
    51. | 10 | 124 | 关羽 | 123456 |
    52. | 11 | 125 | 刘备 | 123459990 |
    53. | 13 | 132 | xuyou | 11111111 |
    54. | 14 | 128 | 许褚 | 1123334455 |
    55. | 15 | 129 | 诸葛亮 | 32111234343 |
    56. | 16 | 130 | 孙权 | 64533764 |
    57. | 17 | 131 | 吕布 | 4232455 |
    58. | 18 | 134 | 貂蝉 | 1112235554 |
    59. | 20 | 140 | 许攸 | 31213554 |
    60. +----+------+-----------+-------------+
    61. 10 rows in set (0.00 sec)
    62. mysql> replace into students (sn,name,qq)values(140,'许攸','31213554');
    63. Query OK, 2 rows affected (0.00 sec)
    64. mysql> select * from students;
    65. +----+------+-----------+-------------+
    66. | id | sn | name | qq |
    67. +----+------+-----------+-------------+
    68. | 1 | 123 | 张飞 | 4567890 |
    69. | 10 | 124 | 关羽 | 123456 |
    70. | 11 | 125 | 刘备 | 123459990 |
    71. | 13 | 132 | xuyou | 11111111 |
    72. | 14 | 128 | 许褚 | 1123334455 |
    73. | 15 | 129 | 诸葛亮 | 32111234343 |
    74. | 16 | 130 | 孙权 | 64533764 |
    75. | 17 | 131 | 吕布 | 4232455 |
    76. | 18 | 134 | 貂蝉 | 1112235554 |
    77. | 21 | 140 | 许攸 | 31213554 |
    78. +----+------+-----------+-------------+
    79. 10 rows in set (0.00 sec)
    80. mysql>

    主键或唯一键没有冲突,则直接插入。

    主键或唯一键有冲突,则删除后插入。

    1 row affected:表中没有冲突数据,数据被插入。

    2 row affectede:表中有冲突数据,删除后重新插入。

    update

    将孙悟空同学的数学成绩变更为80分

     
    
    1. mysql> select name ,math from exam_result ;
    2. +-----------+------+
    3. | name | math |
    4. +-----------+------+
    5. | 唐三藏 | 98 |
    6. | 孙悟空 | 78 |
    7. | 猪悟能 | 98 |
    8. | 曹孟德 | 84 |
    9. | 刘玄德 | 85 |
    10. | 孙权 | 73 |
    11. | 宋公明 | 65 |
    12. +-----------+------+
    13. 7 rows in set (0.00 sec)
    14. mysql> update exam_result set math=80 where name='孙悟空';
    15. Query OK, 1 row affected (0.00 sec)
    16. Rows matched: 1 Changed: 1 Warnings: 0
    17. mysql> select name ,math from exam_result ;
    18. +-----------+------+
    19. | name | math |
    20. +-----------+------+
    21. | 唐三藏 | 98 |
    22. | 孙悟空 | 80 |
    23. | 猪悟能 | 98 |
    24. | 曹孟德 | 84 |
    25. | 刘玄德 | 85 |
    26. | 孙权 | 73 |
    27. | 宋公明 | 65 |
    28. +-----------+------+
    29. 7 rows in set (0.00 sec)
    30. mysql>

    将曹孟德同学的数学成绩变更为60分,语文成绩变更为70分

     
    
    1. mysql> select * from exam_result ;
    2. +----+-----------+---------+------+---------+
    3. | id | name | chinese | math | english |
    4. +----+-----------+---------+------+---------+
    5. | 1 | 唐三藏 | 67 | 98 | 56 |
    6. | 2 | 孙悟空 | 87 | 80 | 77 |
    7. | 3 | 猪悟能 | 88 | 98 | 90 |
    8. | 4 | 曹孟德 | 82 | 84 | 67 |
    9. | 5 | 刘玄德 | 55 | 85 | 45 |
    10. | 6 | 孙权 | 70 | 73 | 78 |
    11. | 7 | 宋公明 | 75 | 65 | 30 |
    12. +----+-----------+---------+------+---------+
    13. 7 rows in set (0.01 sec)
    14. mysql> update exam_result set math=60 ,chinese=70 where name='曹孟德';
    15. Query OK, 1 row affected (0.00 sec)
    16. Rows matched: 1 Changed: 1 Warnings: 0
    17. mysql> select * from exam_result ;
    18. +----+-----------+---------+------+---------+
    19. | id | name | chinese | math | english |
    20. +----+-----------+---------+------+---------+
    21. | 1 | 唐三藏 | 67 | 98 | 56 |
    22. | 2 | 孙悟空 | 87 | 80 | 77 |
    23. | 3 | 猪悟能 | 88 | 98 | 90 |
    24. | 4 | 曹孟德 | 70 | 60 | 67 |
    25. | 5 | 刘玄德 | 55 | 85 | 45 |
    26. | 6 | 孙权 | 70 | 73 | 78 |
    27. | 7 | 宋公明 | 75 | 65 | 30 |
    28. +----+-----------+---------+------+---------+
    29. 7 rows in set (0.00 sec)
    30. mysql>

    将总成绩倒数前三的3位同学的数学成绩加上30分

     
    
    1. mysql> select name , math+english+chinese total from exam_result ;
    2. +-----------+-------+
    3. | name | total |
    4. +-----------+-------+
    5. | 唐三藏 | 221 |
    6. | 孙悟空 | 244 |
    7. | 猪悟能 | 276 |
    8. | 曹孟德 | 197 |
    9. | 刘玄德 | 185 |
    10. | 孙权 | 221 |
    11. | 宋公明 | 170 |
    12. +-----------+-------+
    13. 7 rows in set (0.00 sec)
    14. mysql> select name , math+english+chinese total from exam_result order by total ;
    15. +-----------+-------+
    16. | name | total |
    17. +-----------+-------+
    18. | 宋公明 | 170 |
    19. | 刘玄德 | 185 |
    20. | 曹孟德 | 197 |
    21. | 唐三藏 | 221 |
    22. | 孙权 | 221 |
    23. | 孙悟空 | 244 |
    24. | 猪悟能 | 276 |
    25. +-----------+-------+
    26. 7 rows in set (0.00 sec)
    27. mysql> select name , math+english+chinese total from exam_result order by total desc;
    28. +-----------+-------+
    29. | name | total |
    30. +-----------+-------+
    31. | 猪悟能 | 276 |
    32. | 孙悟空 | 244 |
    33. | 唐三藏 | 221 |
    34. | 孙权 | 221 |
    35. | 曹孟德 | 197 |
    36. | 刘玄德 | 185 |
    37. | 宋公明 | 170 |
    38. +-----------+-------+
    39. 7 rows in set (0.00 sec)
    40. mysql> select name , math+english+chinese total from exam_result order by total desc limit 0,3;
    41. +-----------+-------+
    42. | name | total |
    43. +-----------+-------+
    44. | 猪悟能 | 276 |
    45. | 孙悟空 | 244 |
    46. | 唐三藏 | 221 |
    47. +-----------+-------+
    48. 3 rows in set (0.00 sec)
    49. mysql> select name , math+english+chinese total from exam_result order by total limit 3;
    50. +-----------+-------+
    51. | name | total |
    52. +-----------+-------+
    53. | 宋公明 | 170 |
    54. | 刘玄德 | 185 |
    55. | 曹孟德 | 197 |
    56. +-----------+-------+
    57. 3 rows in set (0.00 sec)
    58. mysql> update exam_result set math=math+30 order by chinese+english+math asc limit 3;
    59. Query OK, 3 rows affected (0.00 sec)
    60. Rows matched: 3 Changed: 3 Warnings: 0
    61. mysql> select name , math+english+chinese total from exam_result order by total limit 3;
    62. +-----------+-------+
    63. | name | total |
    64. +-----------+-------+
    65. | 宋公明 | 200 |
    66. | 刘玄德 | 215 |
    67. | 唐三藏 | 221 |
    68. +-----------+-------+
    69. 3 rows in set (0.00 sec)
    70. mysql> select name ,math+chinese+english total from exam_result order by total;
    71. +-----------+-------+
    72. | name | total |
    73. +-----------+-------+
    74. | 宋公明 | 200 |
    75. | 刘玄德 | 215 |
    76. | 唐三藏 | 221 |
    77. | 孙权 | 221 |
    78. | 曹孟德 | 227 |
    79. | 孙悟空 | 244 |
    80. | 猪悟能 | 276 |
    81. +-----------+-------+
    82. 7 rows in set (0.00 sec)
    83. mysql>

    将所有同学的语文成绩更新为原来的2倍

     
    
    1. mysql> select *from exam_result;
    2. +----+-----------+---------+------+---------+
    3. | id | name | chinese | math | english |
    4. +----+-----------+---------+------+---------+
    5. | 1 | 唐三藏 | 67 | 98 | 56 |
    6. | 2 | 孙悟空 | 87 | 80 | 77 |
    7. | 3 | 猪悟能 | 88 | 98 | 90 |
    8. | 4 | 曹孟德 | 70 | 90 | 67 |
    9. | 5 | 刘玄德 | 55 | 115 | 45 |
    10. | 6 | 孙权 | 70 | 73 | 78 |
    11. | 7 | 宋公明 | 75 | 95 | 30 |
    12. +----+-----------+---------+------+---------+
    13. 7 rows in set (0.00 sec)
    14. mysql> update exam_result set chinese=chinese*2 ;
    15. Query OK, 7 rows affected (0.01 sec)
    16. Rows matched: 7 Changed: 7 Warnings: 0
    17. mysql> select * from exam_result;
    18. +----+-----------+---------+------+---------+
    19. | id | name | chinese | math | english |
    20. +----+-----------+---------+------+---------+
    21. | 1 | 唐三藏 | 134 | 98 | 56 |
    22. | 2 | 孙悟空 | 174 | 80 | 77 |
    23. | 3 | 猪悟能 | 176 | 98 | 90 |
    24. | 4 | 曹孟德 | 140 | 90 | 67 |
    25. | 5 | 刘玄德 | 110 | 115 | 45 |
    26. | 6 | 孙权 | 140 | 73 | 78 |
    27. | 7 | 宋公明 | 150 | 95 | 30 |
    28. +----+-----------+---------+------+---------+
    29. 7 rows in set (0.00 sec)
    30. mysql>

    表之“查”操作(部分)

    全列查询

     
    
    1. mysql> select * from students;
    2. +----+------+-----------+-------------+
    3. | id | sn | name | qq |
    4. +----+------+-----------+-------------+
    5. | 1 | 123 | 张飞 | 4567890 |
    6. | 10 | 124 | 关羽 | 123456 |
    7. | 11 | 125 | 刘备 | 123459990 |
    8. | 13 | 132 | xuyou | 11111111 |
    9. | 14 | 128 | 许褚 | 1123334455 |
    10. | 15 | 129 | 诸葛亮 | 32111234343 |
    11. | 16 | 130 | 孙权 | 64533764 |
    12. | 17 | 131 | 吕布 | 4232455 |
    13. | 18 | 134 | 貂蝉 | 1112235554 |
    14. | 21 | 140 | 许攸 | 31213554 |
    15. +----+------+-----------+-------------+
    16. 10 rows in set (0.00 sec)

    指定列查询

     
    
    1. mysql> select id from students;
    2. +----+
    3. | id |
    4. +----+
    5. | 1 |
    6. | 10 |
    7. | 11 |
    8. | 14 |
    9. | 15 |
    10. | 16 |
    11. | 17 |
    12. | 13 |
    13. | 18 |
    14. | 21 |
    15. +----+
    16. 10 rows in set (0.00 sec)
    17. mysql> select id,sn from students;
    18. +----+------+
    19. | id | sn |
    20. +----+------+
    21. | 1 | 123 |
    22. | 10 | 124 |
    23. | 11 | 125 |
    24. | 14 | 128 |
    25. | 15 | 129 |
    26. | 16 | 130 |
    27. | 17 | 131 |
    28. | 13 | 132 |
    29. | 18 | 134 |
    30. | 21 | 140 |
    31. +----+------+
    32. 10 rows in set (0.00 sec)
    33. mysql> select id,sn,name from students;
    34. +----+------+-----------+
    35. | id | sn | name |
    36. +----+------+-----------+
    37. | 1 | 123 | 张飞 |
    38. | 10 | 124 | 关羽 |
    39. | 11 | 125 | 刘备 |
    40. | 13 | 132 | xuyou |
    41. | 14 | 128 | 许褚 |
    42. | 15 | 129 | 诸葛亮 |
    43. | 16 | 130 | 孙权 |
    44. | 17 | 131 | 吕布 |
    45. | 18 | 134 | 貂蝉 |
    46. | 21 | 140 | 许攸 |
    47. +----+------+-----------+
    48. 10 rows in set (0.00 sec)
    49. mysql> select id,sn,name,qq from students;
    50. +----+------+-----------+-------------+
    51. | id | sn | name | qq |
    52. +----+------+-----------+-------------+
    53. | 1 | 123 | 张飞 | 4567890 |
    54. | 10 | 124 | 关羽 | 123456 |
    55. | 11 | 125 | 刘备 | 123459990 |
    56. | 13 | 132 | xuyou | 11111111 |
    57. | 14 | 128 | 许褚 | 1123334455 |
    58. | 15 | 129 | 诸葛亮 | 32111234343 |
    59. | 16 | 130 | 孙权 | 64533764 |
    60. | 17 | 131 | 吕布 | 4232455 |
    61. | 18 | 134 | 貂蝉 | 1112235554 |
    62. | 21 | 140 | 许攸 | 31213554 |
    63. +----+------+-----------+-------------+
    64. 10 rows in set (0.00 sec)
    65. mysql>

    表达式查询

    示例展示

     
    
    1. mysql> create table exam_result(
    2. -> id int unsigned primary key auto_increment,
    3. -> name varchar(20) not null comment '同学姓名',
    4. -> chinese float default 0.0 comment '语文成绩',
    5. -> math float default 0.0 comment '数学成绩',
    6. -> english float default 0.0 comment '英语乘积'
    7. -> );
    8. Query OK, 0 rows affected (0.05 sec)
    9. mysql> desc exam_result;
    10. +---------+------------------+------+-----+---------+----------------+
    11. | Field | Type | Null | Key | Default | Extra |
    12. +---------+------------------+------+-----+---------+----------------+
    13. | id | int(10) unsigned | NO | PRI | NULL | auto_increment |
    14. | name | varchar(20) | NO | | NULL | |
    15. | chinese | float | YES | | 0 | |
    16. | math | float | YES | | 0 | |
    17. | english | float | YES | | 0 | |
    18. +---------+------------------+------+-----+---------+----------------+
    19. 5 rows in set (0.00 sec)
    20. mysql> insert into exam_result (name ,chinese,math,english)values
    21. -> ('唐三藏',67,98,56),
    22. -> ('孙悟空',87,78,77),
    23. -> ('猪悟能',88,98,90),
    24. -> ('曹孟德',82,84,67),
    25. -> ('刘玄德',55,85,45),
    26. -> ('孙权',70,73,78),
    27. -> ('宋公明',75,65,30);
    28. Query OK, 7 rows affected (0.00 sec)
    29. Records: 7 Duplicates: 0 Warnings: 0
    30. mysql> select * from exam_result;
    31. +----+-----------+---------+------+---------+
    32. | id | name | chinese | math | english |
    33. +----+-----------+---------+------+---------+
    34. | 1 | 唐三藏 | 67 | 98 | 56 |
    35. | 2 | 孙悟空 | 87 | 78 | 77 |
    36. | 3 | 猪悟能 | 88 | 98 | 90 |
    37. | 4 | 曹孟德 | 82 | 84 | 67 |
    38. | 5 | 刘玄德 | 55 | 85 | 45 |
    39. | 6 | 孙权 | 70 | 73 | 78 |
    40. | 7 | 宋公明 | 75 | 65 | 30 |
    41. +----+-----------+---------+------+---------+
    42. 7 rows in set (0.00 sec)
    43. mysql>

    表达式查询

     
    
    1. mysql> select 1+1;
    2. +-----+
    3. | 1+1 |
    4. +-----+
    5. | 2 |
    6. +-----+
    7. 1 row in set (0.00 sec)
    8. mysql> select 7+8;
    9. +-----+
    10. | 7+8 |
    11. +-----+
    12. | 15 |
    13. +-----+
    14. 1 row in set (0.00 sec)
    15. mysql> select name,math,10 from exam_result;
    16. +-----------+------+----+
    17. | name | math | 10 |
    18. +-----------+------+----+
    19. | 唐三藏 | 98 | 10 |
    20. | 孙悟空 | 78 | 10 |
    21. | 猪悟能 | 98 | 10 |
    22. | 曹孟德 | 84 | 10 |
    23. | 刘玄德 | 85 | 10 |
    24. | 孙权 | 73 | 10 |
    25. | 宋公明 | 65 | 10 |
    26. +-----------+------+----+
    27. 7 rows in set (0.00 sec)
    28. mysql> select name,math,1+1 from exam_result;
    29. +-----------+------+-----+
    30. | name | math | 1+1 |
    31. +-----------+------+-----+
    32. | 唐三藏 | 98 | 2 |
    33. | 孙悟空 | 78 | 2 |
    34. | 猪悟能 | 98 | 2 |
    35. | 曹孟德 | 84 | 2 |
    36. | 刘玄德 | 85 | 2 |
    37. | 孙权 | 73 | 2 |
    38. | 宋公明 | 65 | 2 |
    39. +-----------+------+-----+
    40. 7 rows in set (0.00 sec)
    41. mysql> select name,math,math+chinese+english from exam_result;
    42. +-----------+------+----------------------+
    43. | name | math | math+chinese+english |
    44. +-----------+------+----------------------+
    45. | 唐三藏 | 98 | 221 |
    46. | 孙悟空 | 78 | 242 |
    47. | 猪悟能 | 98 | 276 |
    48. | 曹孟德 | 84 | 233 |
    49. | 刘玄德 | 85 | 185 |
    50. | 孙权 | 73 | 221 |
    51. | 宋公明 | 65 | 170 |
    52. +-----------+------+----------------------+
    53. 7 rows in set (0.00 sec)
    54. mysql> select name,math,math+chinese+english as total from exam_result;
    55. +-----------+------+-------+
    56. | name | math | total |
    57. +-----------+------+-------+
    58. | 唐三藏 | 98 | 221 |
    59. | 孙悟空 | 78 | 242 |
    60. | 猪悟能 | 98 | 276 |
    61. | 曹孟德 | 84 | 233 |
    62. | 刘玄德 | 85 | 185 |
    63. | 孙权 | 73 | 221 |
    64. | 宋公明 | 65 | 170 |
    65. +-----------+------+-------+
    66. 7 rows in set (0.00 sec)
    67. mysql> select name,math,math+chinese+english total from exam_result;
    68. +-----------+------+-------+
    69. | name | math | total |
    70. +-----------+------+-------+
    71. | 唐三藏 | 98 | 221 |
    72. | 孙悟空 | 78 | 242 |
    73. | 猪悟能 | 98 | 276 |
    74. | 曹孟德 | 84 | 233 |
    75. | 刘玄德 | 85 | 185 |
    76. | 孙权 | 73 | 221 |
    77. | 宋公明 | 65 | 170 |
    78. +-----------+------+-------+
    79. 7 rows in set (0.00 sec)
    80. mysql> select name 姓名 ,math 数学,math+chinese+english 总分 from exam_result;
    81. +-----------+--------+--------+
    82. | 姓名 | 数学 | 总分 |
    83. +-----------+--------+--------+
    84. | 唐三藏 | 98 | 221 |
    85. | 孙悟空 | 78 | 242 |
    86. | 猪悟能 | 98 | 276 |
    87. | 曹孟德 | 84 | 233 |
    88. | 刘玄德 | 85 | 185 |
    89. | 孙权 | 73 | 221 |
    90. | 宋公明 | 65 | 170 |
    91. +-----------+--------+--------+
    92. 7 rows in set (0.00 sec)
    93. mysql>

    去重查询(distinct)

     
    
    1. mysql> select math from exam_result;
    2. +------+
    3. | math |
    4. +------+
    5. | 98 |
    6. | 78 |
    7. | 98 |
    8. | 84 |
    9. | 85 |
    10. | 73 |
    11. | 65 |
    12. +------+
    13. 7 rows in set (0.00 sec)
    14. mysql> select distinct math from exam_result;
    15. +------+
    16. | math |
    17. +------+
    18. | 98 |
    19. | 78 |
    20. | 84 |
    21. | 85 |
    22. | 73 |
    23. | 65 |
    24. +------+
    25. 6 rows in set (0.00 sec)
    26. mysql>

    WHERE条件

    运算符

    说明

    >,>=,<,<=

    大于,大于等于,小于,小于等于

    =

    等于,NULL不安全,例如NULL=NULL的结果是NULL

    <=>

    等于,NULL安全,例如NULL<=>NULL的结果是TRUE(1)

    !=,<>

    不等于

    BETWEEN a0 AND a1

    范围匹配,[a0,a1],如果a0<=value<=a1,返回TRUE(1)

    IN(option,...)

    如果是option中的任意一个,返回TRUE(1)

    IS NULL

    是NULL

    IS NOT NULL

    不是NULL

    LIKE

    模糊匹配。%表示匹配多个(包括0个)任意字符;_表示任意一个字符

    AND

    多个条件必须为TRUE(1),结果为TRUE(1)

    OR

    任意一个条件为TRUE(1),结果为TRUE(1)

    NOT

    条件为TRUE(1),结果为FALSE(0)

     
    
    1. mysql> select NULL;
    2. +------+
    3. | NULL |
    4. +------+
    5. | NULL |
    6. +------+
    7. 1 row in set (0.00 sec)
    8. mysql> select 0;
    9. +---+
    10. | 0 |
    11. +---+
    12. | 0 |
    13. +---+
    14. 1 row in set (0.00 sec)
    15. mysql> select NULL=NULL;
    16. +-----------+
    17. | NULL=NULL |
    18. +-----------+
    19. | NULL |
    20. +-----------+
    21. 1 row in set (0.00 sec)
    22. mysql> select null<=>null
    23. -> ;
    24. +-------------+
    25. | null<=>null |
    26. +-------------+
    27. | 1 |
    28. +-------------+
    29. 1 row in set (0.00 sec)
    30. mysql> select 1=1
    31. -> ;
    32. +-----+
    33. | 1=1 |
    34. +-----+
    35. | 1 |
    36. +-----+
    37. 1 row in set (0.00 sec)
    38. mysql> select 2=2;
    39. +-----+
    40. | 2=2 |
    41. +-----+
    42. | 1 |
    43. +-----+
    44. 1 row in set (0.00 sec)
    45. mysql> select 2=1;
    46. +-----+
    47. | 2=1 |
    48. +-----+
    49. | 0 |
    50. +-----+
    51. 1 row in set (0.00 sec)
    52. mysql> select 2=null;
    53. +--------+
    54. | 2=null |
    55. +--------+
    56. | NULL |
    57. +--------+
    58. 1 row in set (0.00 sec)
    59. mysql> select 1<=>1;
    60. +-------+
    61. | 1<=>1 |
    62. +-------+
    63. | 1 |
    64. +-------+
    65. 1 row in set (0.00 sec)
    66. mysql> select 1<=>0;
    67. +-------+
    68. | 1<=>0 |
    69. +-------+
    70. | 0 |
    71. +-------+
    72. 1 row in set (0.00 sec)
    73. mysql> select 1!=1;
    74. +------+
    75. | 1!=1 |
    76. +------+
    77. | 0 |
    78. +------+
    79. 1 row in set (0.00 sec)
    80. mysql> select 1!=2;
    81. +------+
    82. | 1!=2 |
    83. +------+
    84. | 1 |
    85. +------+
    86. 1 row in set (0.00 sec)
    87. mysql> select null is null;
    88. +--------------+
    89. | null is null |
    90. +--------------+
    91. | 1 |
    92. +--------------+
    93. 1 row in set (0.00 sec)
    94. mysql> select null is not null;
    95. +------------------+
    96. | null is not null |
    97. +------------------+
    98. | 0 |
    99. +------------------+
    100. 1 row in set (0.00 sec)
    101. mysql>

    条件查询

     
    
    1. mysql> select name , english from exam_result ;
    2. +-----------+---------+
    3. | name | english |
    4. +-----------+---------+
    5. | 唐三藏 | 56 |
    6. | 孙悟空 | 77 |
    7. | 猪悟能 | 90 |
    8. | 曹孟德 | 67 |
    9. | 刘玄德 | 45 |
    10. | 孙权 | 78 |
    11. | 宋公明 | 30 |
    12. +-----------+---------+
    13. 7 rows in set (0.00 sec)
    14. mysql> select name , english from exam_result where english<60;
    15. +-----------+---------+
    16. | name | english |
    17. +-----------+---------+
    18. | 唐三藏 | 56 |
    19. | 刘玄德 | 45 |
    20. | 宋公明 | 30 |
    21. +-----------+---------+
    22. 3 rows in set (0.00 sec)
    23. mysql>

    where条件查询案例

    语文成绩在[80,90]分的同学及语文成绩

     
    
    1. mysql> select * from exam_result;
    2. +----+-----------+---------+------+---------+
    3. | id | name | chinese | math | english |
    4. +----+-----------+---------+------+---------+
    5. | 1 | 唐三藏 | 67 | 98 | 56 |
    6. | 2 | 孙悟空 | 87 | 78 | 77 |
    7. | 3 | 猪悟能 | 88 | 98 | 90 |
    8. | 4 | 曹孟德 | 82 | 84 | 67 |
    9. | 5 | 刘玄德 | 55 | 85 | 45 |
    10. | 6 | 孙权 | 70 | 73 | 78 |
    11. | 7 | 宋公明 | 75 | 65 | 30 |
    12. +----+-----------+---------+------+---------+
    13. 7 rows in set (0.00 sec)
    14. mysql> select name,chinese from exam_result;
    15. +-----------+---------+
    16. | name | chinese |
    17. +-----------+---------+
    18. | 唐三藏 | 67 |
    19. | 孙悟空 | 87 |
    20. | 猪悟能 | 88 |
    21. | 曹孟德 | 82 |
    22. | 刘玄德 | 55 |
    23. | 孙权 | 70 |
    24. | 宋公明 | 75 |
    25. +-----------+---------+
    26. 7 rows in set (0.00 sec)
    27. mysql> select name,chinese from exam_result where chinese>=80 and chinese<=90;
    28. +-----------+---------+
    29. | name | chinese |
    30. +-----------+---------+
    31. | 孙悟空 | 87 |
    32. | 猪悟能 | 88 |
    33. | 曹孟德 | 82 |
    34. +-----------+---------+
    35. 3 rows in set (0.00 sec)
    36. mysql> select name,chinese from exam_result where chinese between 80 and 90;
    37. +-----------+---------+
    38. | name | chinese |
    39. +-----------+---------+
    40. | 孙悟空 | 87 |
    41. | 猪悟能 | 88 |
    42. | 曹孟德 | 82 |
    43. +-----------+---------+
    44. 3 rows in set (0.00 sec)
    45. mysql>

    数学成绩是58或59或98或99的同学及数学成绩

     
    
    1. mysql> select name ,math from exam_result;
    2. +-----------+------+
    3. | name | math |
    4. +-----------+------+
    5. | 唐三藏 | 98 |
    6. | 孙悟空 | 78 |
    7. | 猪悟能 | 98 |
    8. | 曹孟德 | 84 |
    9. | 刘玄德 | 85 |
    10. | 孙权 | 73 |
    11. | 宋公明 | 65 |
    12. +-----------+------+
    13. 7 rows in set (0.00 sec)
    14. mysql> select name ,math from exam_result where math=58 or math =59 or math =98 or math =99;
    15. +-----------+------+
    16. | name | math |
    17. +-----------+------+
    18. | 唐三藏 | 98 |
    19. | 猪悟能 | 98 |
    20. +-----------+------+
    21. 2 rows in set (0.00 sec)
    22. mysql> select name ,math from exam_result where math in(58,59,98,99);
    23. +-----------+------+
    24. | name | math |
    25. +-----------+------+
    26. | 唐三藏 | 98 |
    27. | 猪悟能 | 98 |
    28. +-----------+------+
    29. 2 rows in set (0.00 sec)
    30. mysql>

    姓孙的同学及孙某同学

     
    
    1. mysql> select name from exam_result where name like '孙%';
    2. +-----------+
    3. | name |
    4. +-----------+
    5. | 孙悟空 |
    6. | 孙权 |
    7. +-----------+
    8. 2 rows in set (0.00 sec)
    9. mysql> select name from exam_result where name like '孙_';
    10. +--------+
    11. | name |
    12. +--------+
    13. | 孙权 |
    14. +--------+
    15. 1 row in set (0.00 sec)
    16. mysql>

    语文成绩好于英语成绩的同学

     
    
    1. mysql> select name ,chinese,english from exam_result where chinese>english;
    2. +-----------+---------+---------+
    3. | name | chinese | english |
    4. +-----------+---------+---------+
    5. | 唐三藏 | 67 | 56 |
    6. | 孙悟空 | 87 | 77 |
    7. | 曹孟德 | 82 | 67 |
    8. | 刘玄德 | 55 | 45 |
    9. | 宋公明 | 75 | 30 |
    10. +-----------+---------+---------+
    11. 5 rows in set (0.00 sec)
    12. mysql>

    总分在200分以下的同学

     
    
    1. mysql> select name ,chinese+math+english from exam_result;
    2. +-----------+----------------------+
    3. | name | chinese+math+english |
    4. +-----------+----------------------+
    5. | 唐三藏 | 221 |
    6. | 孙悟空 | 242 |
    7. | 猪悟能 | 276 |
    8. | 曹孟德 | 233 |
    9. | 刘玄德 | 185 |
    10. | 孙权 | 221 |
    11. | 宋公明 | 170 |
    12. +-----------+----------------------+
    13. 7 rows in set (0.00 sec)
    14. mysql> select name ,chinese+math+english from exam_result where chinese+math+english<200;
    15. +-----------+----------------------+
    16. | name | chinese+math+english |
    17. +-----------+----------------------+
    18. | 刘玄德 | 185 |
    19. | 宋公明 | 170 |
    20. +-----------+----------------------+
    21. 2 rows in set (0.00 sec)
    22. mysql> select name ,chinese+math+english total from exam_result ;
    23. +-----------+-------+
    24. | name | total |
    25. +-----------+-------+
    26. | 唐三藏 | 221 |
    27. | 孙悟空 | 242 |
    28. | 猪悟能 | 276 |
    29. | 曹孟德 | 233 |
    30. | 刘玄德 | 185 |
    31. | 孙权 | 221 |
    32. | 宋公明 | 170 |
    33. +-----------+-------+
    34. 7 rows in set (0.00 sec)
    35. mysql>

    mysql语句执行顺序

     
    
    1. mysql> select name ,chinese+math+english total from exam_result where total<200;
    2. ERROR 1054 (42S22): Unknown column 'total' in 'where clause'
    3. mysql> select name ,chinese+math+english total from exam_result where chinese+math+english<200;
    4. +-----------+-------+
    5. | name | total |
    6. +-----------+-------+
    7. | 刘玄德 | 185 |
    8. | 宋公明 | 170 |
    9. +-----------+-------+
    10. 2 rows in set (0.00 sec)
    11. mysql> select name ,chinese+math+english as total from exam_result where chinese+math+english<200;
    12. +-----------+-------+
    13. | name | total |
    14. +-----------+-------+
    15. | 刘玄德 | 185 |
    16. | 宋公明 | 170 |
    17. +-----------+-------+
    18. 2 rows in set (0.00 sec)
    19. mysql>

    首先要知道在哪一个表中做操作,第二步是条件判断,前两步得出的是符合条件的表,第三步在得到的符合条件的表中选择列数据。在执行第二步的时候,并不知道total是什么。

    语文成绩>80并且不姓孙的同学

     
    
    1. mysql> select name ,chinese from exam_result where chinese>80;
    2. +-----------+---------+
    3. | name | chinese |
    4. +-----------+---------+
    5. | 孙悟空 | 87 |
    6. | 猪悟能 | 88 |
    7. | 曹孟德 | 82 |
    8. +-----------+---------+
    9. 3 rows in set (0.00 sec)
    10. mysql> select name ,chinese from exam_result where name like '孙%';
    11. +-----------+---------+
    12. | name | chinese |
    13. +-----------+---------+
    14. | 孙悟空 | 87 |
    15. | 孙权 | 70 |
    16. +-----------+---------+
    17. 2 rows in set (0.00 sec)
    18. mysql> select name ,chinese from exam_result where name not like '孙%';
    19. +-----------+---------+
    20. | name | chinese |
    21. +-----------+---------+
    22. | 唐三藏 | 67 |
    23. | 猪悟能 | 88 |
    24. | 曹孟德 | 82 |
    25. | 刘玄德 | 55 |
    26. | 宋公明 | 75 |
    27. +-----------+---------+
    28. 5 rows in set (0.00 sec)
    29. mysql> select name ,chinese from exam_result where chinese>80 and name not like '孙%';
    30. +-----------+---------+
    31. | name | chinese |
    32. +-----------+---------+
    33. | 猪悟能 | 88 |
    34. | 曹孟德 | 82 |
    35. +-----------+---------+
    36. 2 rows in set (0.00 sec)
    37. mysql>

    孙某同学,否则要求总成绩>200并且语文成绩<数学成绩并且英语成绩>80

     
    
    1. mysql> select name ,chinese ,math,english ,chinese+math+english 总分 from exam_result;
    2. +-----------+---------+------+---------+--------+
    3. | name | chinese | math | english | 总分 |
    4. +-----------+---------+------+---------+--------+
    5. | 唐三藏 | 67 | 98 | 56 | 221 |
    6. | 孙悟空 | 87 | 78 | 77 | 242 |
    7. | 猪悟能 | 88 | 98 | 90 | 276 |
    8. | 曹孟德 | 82 | 84 | 67 | 233 |
    9. | 刘玄德 | 55 | 85 | 45 | 185 |
    10. | 孙权 | 70 | 73 | 78 | 221 |
    11. | 宋公明 | 75 | 65 | 30 | 170 |
    12. +-----------+---------+------+---------+--------+
    13. 7 rows in set (0.00 sec)
    14. mysql> select name ,chinese ,math,english ,chinese+math+english 总分 from exam_result where chinese +math+english >200;
    15. +-----------+---------+------+---------+--------+
    16. | name | chinese | math | english | 总分 |
    17. +-----------+---------+------+---------+--------+
    18. | 唐三藏 | 67 | 98 | 56 | 221 |
    19. | 孙悟空 | 87 | 78 | 77 | 242 |
    20. | 猪悟能 | 88 | 98 | 90 | 276 |
    21. | 曹孟德 | 82 | 84 | 67 | 233 |
    22. | 孙权 | 70 | 73 | 78 | 221 |
    23. +-----------+---------+------+---------+--------+
    24. 5 rows in set (0.00 sec)
    25. mysql> select name ,chinese ,math,english ,chinese+math+english 总分 from exam_result where chinese +math+english >200 and chinese <math;
    26. +-----------+---------+------+---------+--------+
    27. | name | chinese | math | english | 总分 |
    28. +-----------+---------+------+---------+--------+
    29. | 唐三藏 | 67 | 98 | 56 | 221 |
    30. | 猪悟能 | 88 | 98 | 90 | 276 |
    31. | 曹孟德 | 82 | 84 | 67 | 233 |
    32. | 孙权 | 70 | 73 | 78 | 221 |
    33. +-----------+---------+------+---------+--------+
    34. 4 rows in set (0.00 sec)
    35. mysql> select name ,chinese ,math,english ,chinese+math+english 总分 from exam_result where chinese +math+english >200 and chinese <math and english>80;
    36. +-----------+---------+------+---------+--------+
    37. | name | chinese | math | english | 总分 |
    38. +-----------+---------+------+---------+--------+
    39. | 猪悟能 | 88 | 98 | 90 | 276 |
    40. +-----------+---------+------+---------+--------+
    41. 1 row in set (0.00 sec)
    42. mysql> select name ,chinese ,math,english ,chinese+math+english 总分 from exam_result where name like '孙_';
    43. +--------+---------+------+---------+--------+
    44. | name | chinese | math | english | 总分 |
    45. +--------+---------+------+---------+--------+
    46. | 孙权 | 70 | 73 | 78 | 221 |
    47. +--------+---------+------+---------+--------+
    48. 1 row in set (0.00 sec)
    49. mysql> select name ,chinese ,math,english ,chinese+math+english 总分 from exam_result where name like '孙_' or chinese +math+english >200 and chinese <math and english>80
    50. 0;
    51. +-----------+---------+------+---------+--------+
    52. | name | chinese | math | english | 总分 |
    53. +-----------+---------+------+---------+--------+
    54. | 猪悟能 | 88 | 98 | 90 | 276 |
    55. | 孙权 | 70 | 73 | 78 | 221 |
    56. +-----------+---------+------+---------+--------+
    57. 2 rows in set (0.00 sec)
    58. mysql>

    NULL和''的区别

     
    
    1. mysql> create table test_null(
    2. -> id int,
    3. -> name varchar (20)
    4. -> );
    5. Query OK, 0 rows affected (0.03 sec)
    6. mysql>
    7. mysql> insert into test_null (id,name) values (1,'张三');
    8. Query OK, 1 row affected (0.01 sec)
    9. mysql> insert into test_null (id,name) values (null,'张三');
    10. Query OK, 1 row affected (0.00 sec)
    11. mysql> insert into test_null (id,name) values (1,null);
    12. Query OK, 1 row affected (0.01 sec)
    13. mysql> insert into test_null (id,name) values (null,null);
    14. Query OK, 1 row affected (0.01 sec)
    15. mysql> insert into test_null (id,name) values (1,'');
    16. Query OK, 1 row affected (0.01 sec)
    17. mysql> select * from test_null;
    18. +------+--------+
    19. | id | name |
    20. +------+--------+
    21. | 1 | 张三 |
    22. | NULL | 张三 |
    23. | 1 | NULL |
    24. | NULL | NULL |
    25. | 1 | |
    26. +------+--------+
    27. 5 rows in set (0.00 sec)
    28. mysql> select * from test_null where name is null;
    29. +------+------+
    30. | id | name |
    31. +------+------+
    32. | 1 | NULL |
    33. | NULL | NULL |
    34. +------+------+
    35. 2 rows in set (0.00 sec)
    36. mysql> select * from test_null where name = '';
    37. +------+------+
    38. | id | name |
    39. +------+------+
    40. | 1 | |
    41. +------+------+
    42. 1 row in set (0.00 sec)
    43. mysql> select * from test_null where name is not null;
    44. +------+--------+
    45. | id | name |
    46. +------+--------+
    47. | 1 | 张三 |
    48. | NULL | 张三 |
    49. | 1 | |
    50. +------+--------+
    51. 3 rows in set (0.00 sec)
    52. mysql>

    Order by

    asc升序

    desc降序

    默认升序

    同学及数学成绩,按照数学成绩升序显示

     
    
    1. mysql> select * from exam_result;
    2. +----+-----------+---------+------+---------+
    3. | id | name | chinese | math | english |
    4. +----+-----------+---------+------+---------+
    5. | 1 | 唐三藏 | 67 | 98 | 56 |
    6. | 2 | 孙悟空 | 87 | 78 | 77 |
    7. | 3 | 猪悟能 | 88 | 98 | 90 |
    8. | 4 | 曹孟德 | 82 | 84 | 67 |
    9. | 5 | 刘玄德 | 55 | 85 | 45 |
    10. | 6 | 孙权 | 70 | 73 | 78 |
    11. | 7 | 宋公明 | 75 | 65 | 30 |
    12. +----+-----------+---------+------+---------+
    13. 7 rows in set (0.00 sec)
    14. mysql> select name ,math from exam_result order by math asc;
    15. +-----------+------+
    16. | name | math |
    17. +-----------+------+
    18. | 宋公明 | 65 |
    19. | 孙权 | 73 |
    20. | 孙悟空 | 78 |
    21. | 曹孟德 | 84 |
    22. | 刘玄德 | 85 |
    23. | 唐三藏 | 98 |
    24. | 猪悟能 | 98 |
    25. +-----------+------+
    26. 7 rows in set (0.00 sec)
    27. mysql>

    NULL的排序

     
    
    1. mysql> select * from test_null;
    2. +------+--------+
    3. | id | name |
    4. +------+--------+
    5. | 1 | 张三 |
    6. | NULL | 张三 |
    7. | 1 | NULL |
    8. | NULL | NULL |
    9. | 1 | |
    10. +------+--------+
    11. 5 rows in set (0.00 sec)
    12. mysql> select * from test_null order by name asc;
    13. +------+--------+
    14. | id | name |
    15. +------+--------+
    16. | 1 | NULL |
    17. | NULL | NULL |
    18. | 1 | |
    19. | 1 | 张三 |
    20. | NULL | 张三 |
    21. +------+--------+
    22. 5 rows in set (0.00 sec)
    23. mysql> select * from test_null order by name desc;
    24. +------+--------+
    25. | id | name |
    26. +------+--------+
    27. | 1 | 张三 |
    28. | NULL | 张三 |
    29. | 1 | |
    30. | 1 | NULL |
    31. | NULL | NULL |
    32. +------+--------+
    33. 5 rows in set (0.00 sec)
    34. mysql>

    NULL在排序中视作为比任何值都要小。

    查询同学各门成绩,依次按数学降序,英语升序,语文升序的方式显示

     
    
    1. mysql> select name ,math,english,chinese from exam_result;
    2. +-----------+------+---------+---------+
    3. | name | math | english | chinese |
    4. +-----------+------+---------+---------+
    5. | 唐三藏 | 98 | 56 | 67 |
    6. | 孙悟空 | 78 | 77 | 87 |
    7. | 猪悟能 | 98 | 90 | 88 |
    8. | 曹孟德 | 84 | 67 | 82 |
    9. | 刘玄德 | 85 | 45 | 55 |
    10. | 孙权 | 73 | 78 | 70 |
    11. | 宋公明 | 65 | 30 | 75 |
    12. +-----------+------+---------+---------+
    13. 7 rows in set (0.00 sec)
    14. mysql> select name ,math,english,chinese from exam_result order by math;
    15. +-----------+------+---------+---------+
    16. | name | math | english | chinese |
    17. +-----------+------+---------+---------+
    18. | 宋公明 | 65 | 30 | 75 |
    19. | 孙权 | 73 | 78 | 70 |
    20. | 孙悟空 | 78 | 77 | 87 |
    21. | 曹孟德 | 84 | 67 | 82 |
    22. | 刘玄德 | 85 | 45 | 55 |
    23. | 唐三藏 | 98 | 56 | 67 |
    24. | 猪悟能 | 98 | 90 | 88 |
    25. +-----------+------+---------+---------+
    26. 7 rows in set (0.00 sec)
    27. mysql> select name ,math,english,chinese from exam_result order by math desc;
    28. +-----------+------+---------+---------+
    29. | name | math | english | chinese |
    30. +-----------+------+---------+---------+
    31. | 唐三藏 | 98 | 56 | 67 |
    32. | 猪悟能 | 98 | 90 | 88 |
    33. | 刘玄德 | 85 | 45 | 55 |
    34. | 曹孟德 | 84 | 67 | 82 |
    35. | 孙悟空 | 78 | 77 | 87 |
    36. | 孙权 | 73 | 78 | 70 |
    37. | 宋公明 | 65 | 30 | 75 |
    38. +-----------+------+---------+---------+
    39. 7 rows in set (0.00 sec)
    40. mysql> select name ,math,english,chinese from exam_result order by math desc,english desc;
    41. +-----------+------+---------+---------+
    42. | name | math | english | chinese |
    43. +-----------+------+---------+---------+
    44. | 猪悟能 | 98 | 90 | 88 |
    45. | 唐三藏 | 98 | 56 | 67 |
    46. | 刘玄德 | 85 | 45 | 55 |
    47. | 曹孟德 | 84 | 67 | 82 |
    48. | 孙悟空 | 78 | 77 | 87 |
    49. | 孙权 | 73 | 78 | 70 |
    50. | 宋公明 | 65 | 30 | 75 |
    51. +-----------+------+---------+---------+
    52. 7 rows in set (0.00 sec)
    53. mysql> select name ,math,english,chinese from exam_result order by math desc,english desc,chinese;
    54. +-----------+------+---------+---------+
    55. | name | math | english | chinese |
    56. +-----------+------+---------+---------+
    57. | 猪悟能 | 98 | 90 | 88 |
    58. | 唐三藏 | 98 | 56 | 67 |
    59. | 刘玄德 | 85 | 45 | 55 |
    60. | 曹孟德 | 84 | 67 | 82 |
    61. | 孙悟空 | 78 | 77 | 87 |
    62. | 孙权 | 73 | 78 | 70 |
    63. | 宋公明 | 65 | 30 | 75 |
    64. +-----------+------+---------+---------+
    65. 7 rows in set (0.00 sec)
    66. mysql> select name ,math,english,chinese from exam_result order by math desc,english desc,chinese asc;
    67. +-----------+------+---------+---------+
    68. | name | math | english | chinese |
    69. +-----------+------+---------+---------+
    70. | 猪悟能 | 98 | 90 | 88 |
    71. | 唐三藏 | 98 | 56 | 67 |
    72. | 刘玄德 | 85 | 45 | 55 |
    73. | 曹孟德 | 84 | 67 | 82 |
    74. | 孙悟空 | 78 | 77 | 87 |
    75. | 孙权 | 73 | 78 | 70 |
    76. | 宋公明 | 65 | 30 | 75 |
    77. +-----------+------+---------+---------+
    78. 7 rows in set (0.00 sec)
    79. mysql>

    默认的排序

    默认升序

     
    
    1. mysql> select name,math from exam_result;
    2. +-----------+------+
    3. | name | math |
    4. +-----------+------+
    5. | 唐三藏 | 98 |
    6. | 孙悟空 | 78 |
    7. | 猪悟能 | 98 |
    8. | 曹孟德 | 84 |
    9. | 刘玄德 | 85 |
    10. | 孙权 | 73 |
    11. | 宋公明 | 65 |
    12. +-----------+------+
    13. 7 rows in set (0.00 sec)
    14. mysql> select name,math from exam_result order by math desc;
    15. +-----------+------+
    16. | name | math |
    17. +-----------+------+
    18. | 唐三藏 | 98 |
    19. | 猪悟能 | 98 |
    20. | 刘玄德 | 85 |
    21. | 曹孟德 | 84 |
    22. | 孙悟空 | 78 |
    23. | 孙权 | 73 |
    24. | 宋公明 | 65 |
    25. +-----------+------+
    26. 7 rows in set (0.00 sec)
    27. mysql> select name,math from exam_result order by math asc;
    28. +-----------+------+
    29. | name | math |
    30. +-----------+------+
    31. | 宋公明 | 65 |
    32. | 孙权 | 73 |
    33. | 孙悟空 | 78 |
    34. | 曹孟德 | 84 |
    35. | 刘玄德 | 85 |
    36. | 唐三藏 | 98 |
    37. | 猪悟能 | 98 |
    38. +-----------+------+
    39. 7 rows in set (0.00 sec)
    40. mysql> select name,math from exam_result order by math ;
    41. +-----------+------+
    42. | name | math |
    43. +-----------+------+
    44. | 宋公明 | 65 |
    45. | 孙权 | 73 |
    46. | 孙悟空 | 78 |
    47. | 曹孟德 | 84 |
    48. | 刘玄德 | 85 |
    49. | 唐三藏 | 98 |
    50. | 猪悟能 | 98 |
    51. +-----------+------+
    52. 7 rows in set (0.00 sec)
    53. mysql>

    查询同学及总分,由高到低(mysql语句执行顺序)

     
    
    1. mysql> select name ,math+english+chinese from exam_result;
    2. +-----------+----------------------+
    3. | name | math+english+chinese |
    4. +-----------+----------------------+
    5. | 唐三藏 | 221 |
    6. | 孙悟空 | 242 |
    7. | 猪悟能 | 276 |
    8. | 曹孟德 | 233 |
    9. | 刘玄德 | 185 |
    10. | 孙权 | 221 |
    11. | 宋公明 | 170 |
    12. +-----------+----------------------+
    13. 7 rows in set (0.00 sec)
    14. mysql> select name ,math+english+chinese as total from exam_result;
    15. +-----------+-------+
    16. | name | total |
    17. +-----------+-------+
    18. | 唐三藏 | 221 |
    19. | 孙悟空 | 242 |
    20. | 猪悟能 | 276 |
    21. | 曹孟德 | 233 |
    22. | 刘玄德 | 185 |
    23. | 孙权 | 221 |
    24. | 宋公明 | 170 |
    25. +-----------+-------+
    26. 7 rows in set (0.01 sec)
    27. mysql> select name ,math+english+chinese as total from exam_result order by total;
    28. +-----------+-------+
    29. | name | total |
    30. +-----------+-------+
    31. | 宋公明 | 170 |
    32. | 刘玄德 | 185 |
    33. | 唐三藏 | 221 |
    34. | 孙权 | 221 |
    35. | 曹孟德 | 233 |
    36. | 孙悟空 | 242 |
    37. | 猪悟能 | 276 |
    38. +-----------+-------+
    39. 7 rows in set (0.00 sec)
    40. mysql> select name ,math+english+chinese as total from exam_result order by total desc;
    41. +-----------+-------+
    42. | name | total |
    43. +-----------+-------+
    44. | 猪悟能 | 276 |
    45. | 孙悟空 | 242 |
    46. | 曹孟德 | 233 |
    47. | 唐三藏 | 221 |
    48. | 孙权 | 221 |
    49. | 刘玄德 | 185 |
    50. | 宋公明 | 170 |
    51. +-----------+-------+
    52. 7 rows in set (0.00 sec)
    53. mysql>

    查询姓孙的同学或者姓曹的同学数学成绩,结果按数学成绩由高到低显示

     
    
    1. mysql> select name ,math from exam_result where name like '孙%' or name like '曹%';
    2. +-----------+------+
    3. | name | math |
    4. +-----------+------+
    5. | 孙悟空 | 78 |
    6. | 曹孟德 | 84 |
    7. | 孙权 | 73 |
    8. +-----------+------+
    9. 3 rows in set (0.00 sec)
    10. mysql> select name ,math from exam_result where name like '孙%' or name like '曹%' order by math;
    11. +-----------+------+
    12. | name | math |
    13. +-----------+------+
    14. | 孙权 | 73 |
    15. | 孙悟空 | 78 |
    16. | 曹孟德 | 84 |
    17. +-----------+------+
    18. 3 rows in set (0.00 sec)
    19. mysql> select name ,math from exam_result where name like '孙%' or name like '曹%' order by math desc;
    20. +-----------+------+
    21. | name | math |
    22. +-----------+------+
    23. | 曹孟德 | 84 |
    24. | 孙悟空 | 78 |
    25. | 孙权 | 73 |
    26. +-----------+------+
    27. 3 rows in set (0.00 sec)
    28. mysql> select * from exam_result limit 3 offset 1;
    29. +----+-----------+---------+------+---------+
    30. | id | name | chinese | math | english |
    31. +----+-----------+---------+------+---------+
    32. | 2 | 孙悟空 | 87 | 78 | 77 |
    33. | 3 | 猪悟能 | 88 | 98 | 90 |
    34. | 4 | 曹孟德 | 82 | 84 | 67 |
    35. +----+-----------+---------+------+---------+
    36. 3 rows in set (0.00 sec)
    37. mysql> select * from exam_result limit 5 offset 0;
    38. +----+-----------+---------+------+---------+
    39. | id | name | chinese | math | english |
    40. +----+-----------+---------+------+---------+
    41. | 1 | 唐三藏 | 67 | 98 | 56 |
    42. | 2 | 孙悟空 | 87 | 78 | 77 |
    43. | 3 | 猪悟能 | 88 | 98 | 90 |
    44. | 4 | 曹孟德 | 82 | 84 | 67 |
    45. | 5 | 刘玄德 | 55 | 85 | 45 |
    46. +----+-----------+---------+------+---------+
    47. 5 rows in set (0.00 sec)
    48. mysql>

    筛选分页

    limit的使用

     
    
    1. mysql> select * from exam_result;
    2. +----+-----------+---------+------+---------+
    3. | id | name | chinese | math | english |
    4. +----+-----------+---------+------+---------+
    5. | 1 | 唐三藏 | 67 | 98 | 56 |
    6. | 2 | 孙悟空 | 87 | 78 | 77 |
    7. | 3 | 猪悟能 | 88 | 98 | 90 |
    8. | 4 | 曹孟德 | 82 | 84 | 67 |
    9. | 5 | 刘玄德 | 55 | 85 | 45 |
    10. | 6 | 孙权 | 70 | 73 | 78 |
    11. | 7 | 宋公明 | 75 | 65 | 30 |
    12. +----+-----------+---------+------+---------+
    13. 7 rows in set (0.00 sec)
    14. mysql> select * from exam_result limit 5;
    15. +----+-----------+---------+------+---------+
    16. | id | name | chinese | math | english |
    17. +----+-----------+---------+------+---------+
    18. | 1 | 唐三藏 | 67 | 98 | 56 |
    19. | 2 | 孙悟空 | 87 | 78 | 77 |
    20. | 3 | 猪悟能 | 88 | 98 | 90 |
    21. | 4 | 曹孟德 | 82 | 84 | 67 |
    22. | 5 | 刘玄德 | 55 | 85 | 45 |
    23. +----+-----------+---------+------+---------+
    24. 5 rows in set (0.00 sec)
    25. mysql> select * from exam_result limit 1;
    26. +----+-----------+---------+------+---------+
    27. | id | name | chinese | math | english |
    28. +----+-----------+---------+------+---------+
    29. | 1 | 唐三藏 | 67 | 98 | 56 |
    30. +----+-----------+---------+------+---------+
    31. 1 row in set (0.00 sec)
    32. mysql> select * from exam_result limit 2;
    33. +----+-----------+---------+------+---------+
    34. | id | name | chinese | math | english |
    35. +----+-----------+---------+------+---------+
    36. | 1 | 唐三藏 | 67 | 98 | 56 |
    37. | 2 | 孙悟空 | 87 | 78 | 77 |
    38. +----+-----------+---------+------+---------+
    39. 2 rows in set (0.00 sec)
    40. mysql> select * from exam_result limit 3;
    41. +----+-----------+---------+------+---------+
    42. | id | name | chinese | math | english |
    43. +----+-----------+---------+------+---------+
    44. | 1 | 唐三藏 | 67 | 98 | 56 |
    45. | 2 | 孙悟空 | 87 | 78 | 77 |
    46. | 3 | 猪悟能 | 88 | 98 | 90 |
    47. +----+-----------+---------+------+---------+
    48. 3 rows in set (0.00 sec)
    49. mysql> select * from exam_result limit 1,3;
    50. +----+-----------+---------+------+---------+
    51. | id | name | chinese | math | english |
    52. +----+-----------+---------+------+---------+
    53. | 2 | 孙悟空 | 87 | 78 | 77 |
    54. | 3 | 猪悟能 | 88 | 98 | 90 |
    55. | 4 | 曹孟德 | 82 | 84 | 67 |
    56. +----+-----------+---------+------+---------+
    57. 3 rows in set (0.00 sec)
    58. mysql> select * from exam_result limit 2,4;
    59. +----+-----------+---------+------+---------+
    60. | id | name | chinese | math | english |
    61. +----+-----------+---------+------+---------+
    62. | 3 | 猪悟能 | 88 | 98 | 90 |
    63. | 4 | 曹孟德 | 82 | 84 | 67 |
    64. | 5 | 刘玄德 | 55 | 85 | 45 |
    65. | 6 | 孙权 | 70 | 73 | 78 |
    66. +----+-----------+---------+------+---------+
    67. 4 rows in set (0.00 sec)
    68. mysql> select * from exam_result limit 2,1;
    69. +----+-----------+---------+------+---------+
    70. | id | name | chinese | math | english |
    71. +----+-----------+---------+------+---------+
    72. | 3 | 猪悟能 | 88 | 98 | 90 |
    73. +----+-----------+---------+------+---------+
    74. 1 row in set (0.00 sec)
    75. mysql> select * from exam_result limit 2,2;
    76. +----+-----------+---------+------+---------+
    77. | id | name | chinese | math | english |
    78. +----+-----------+---------+------+---------+
    79. | 3 | 猪悟能 | 88 | 98 | 90 |
    80. | 4 | 曹孟德 | 82 | 84 | 67 |
    81. +----+-----------+---------+------+---------+
    82. 2 rows in set (0.00 sec)
    83. mysql> select * from exam_result limit 2,3;
    84. +----+-----------+---------+------+---------+
    85. | id | name | chinese | math | english |
    86. +----+-----------+---------+------+---------+
    87. | 3 | 猪悟能 | 88 | 98 | 90 |
    88. | 4 | 曹孟德 | 82 | 84 | 67 |
    89. | 5 | 刘玄德 | 55 | 85 | 45 |
    90. +----+-----------+---------+------+---------+
    91. 3 rows in set (0.00 sec)
    92. mysql> select * from exam_result limit 2,4;
    93. +----+-----------+---------+------+---------+
    94. | id | name | chinese | math | english |
    95. +----+-----------+---------+------+---------+
    96. | 3 | 猪悟能 | 88 | 98 | 90 |
    97. | 4 | 曹孟德 | 82 | 84 | 67 |
    98. | 5 | 刘玄德 | 55 | 85 | 45 |
    99. | 6 | 孙权 | 70 | 73 | 78 |
    100. +----+-----------+---------+------+---------+
    101. 4 rows in set (0.00 sec)
    102. mysql> select * from exam_result limit 0,4;
    103. +----+-----------+---------+------+---------+
    104. | id | name | chinese | math | english |
    105. +----+-----------+---------+------+---------+
    106. | 1 | 唐三藏 | 67 | 98 | 56 |
    107. | 2 | 孙悟空 | 87 | 78 | 77 |
    108. | 3 | 猪悟能 | 88 | 98 | 90 |
    109. | 4 | 曹孟德 | 82 | 84 | 67 |
    110. +----+-----------+---------+------+---------+
    111. 4 rows in set (0.00 sec)
    112. mysql>

    分页的具体操作

     
    
    1. mysql> select * from exam_result;
    2. +----+-----------+---------+------+---------+
    3. | id | name | chinese | math | english |
    4. +----+-----------+---------+------+---------+
    5. | 1 | 唐三藏 | 67 | 98 | 56 |
    6. | 2 | 孙悟空 | 87 | 78 | 77 |
    7. | 3 | 猪悟能 | 88 | 98 | 90 |
    8. | 4 | 曹孟德 | 82 | 84 | 67 |
    9. | 5 | 刘玄德 | 55 | 85 | 45 |
    10. | 6 | 孙权 | 70 | 73 | 78 |
    11. | 7 | 宋公明 | 75 | 65 | 30 |
    12. +----+-----------+---------+------+---------+
    13. 7 rows in set (0.00 sec)
    14. mysql> select * from exam_result limit 0,3;
    15. +----+-----------+---------+------+---------+
    16. | id | name | chinese | math | english |
    17. +----+-----------+---------+------+---------+
    18. | 1 | 唐三藏 | 67 | 98 | 56 |
    19. | 2 | 孙悟空 | 87 | 78 | 77 |
    20. | 3 | 猪悟能 | 88 | 98 | 90 |
    21. +----+-----------+---------+------+---------+
    22. 3 rows in set (0.00 sec)
    23. mysql> select * from exam_result limit 3,3;
    24. +----+-----------+---------+------+---------+
    25. | id | name | chinese | math | english |
    26. +----+-----------+---------+------+---------+
    27. | 4 | 曹孟德 | 82 | 84 | 67 |
    28. | 5 | 刘玄德 | 55 | 85 | 45 |
    29. | 6 | 孙权 | 70 | 73 | 78 |
    30. +----+-----------+---------+------+---------+
    31. 3 rows in set (0.00 sec)
    32. mysql> select * from exam_result limit 6,3;
    33. +----+-----------+---------+------+---------+
    34. | id | name | chinese | math | english |
    35. +----+-----------+---------+------+---------+
    36. | 7 | 宋公明 | 75 | 65 | 30 |
    37. +----+-----------+---------+------+---------+
    38. 1 row in set (0.00 sec)
    39. mysql> select * from exam_result limit 3 offset 0;
    40. +----+-----------+---------+------+---------+
    41. | id | name | chinese | math | english |
    42. +----+-----------+---------+------+---------+
    43. | 1 | 唐三藏 | 67 | 98 | 56 |
    44. | 2 | 孙悟空 | 87 | 78 | 77 |
    45. | 3 | 猪悟能 | 88 | 98 | 90 |
    46. +----+-----------+---------+------+---------+
    47. 3 rows in set (0.00 sec)
    48. mysql> select * from exam_result limit 3 offset 3;
    49. +----+-----------+---------+------+---------+
    50. | id | name | chinese | math | english |
    51. +----+-----------+---------+------+---------+
    52. | 4 | 曹孟德 | 82 | 84 | 67 |
    53. | 5 | 刘玄德 | 55 | 85 | 45 |
    54. | 6 | 孙权 | 70 | 73 | 78 |
    55. +----+-----------+---------+------+---------+
    56. 3 rows in set (0.00 sec)
    57. mysql> select * from exam_result limit 3 offset 6;
    58. +----+-----------+---------+------+---------+
    59. | id | name | chinese | math | english |
    60. +----+-----------+---------+------+---------+
    61. | 7 | 宋公明 | 75 | 65 | 30 |
    62. +----+-----------+---------+------+---------+
    63. 1 row in set (0.00 sec)
    64. mysql>

    同学名字及总分,总分大于200分,总分降序排序,分页操作

     
    
    1. mysql> select name ,math+english+chinese total from exam_result where english+math+chinese >200 order by total;
    2. +-----------+-------+
    3. | name | total |
    4. +-----------+-------+
    5. | 唐三藏 | 221 |
    6. | 孙权 | 221 |
    7. | 曹孟德 | 233 |
    8. | 孙悟空 | 242 |
    9. | 猪悟能 | 276 |
    10. +-----------+-------+
    11. 5 rows in set (0.00 sec)
    12. mysql> select name ,math+english+chinese total from exam_result where english+math+chinese >200 order by total desc;
    13. +-----------+-------+
    14. | name | total |
    15. +-----------+-------+
    16. | 猪悟能 | 276 |
    17. | 孙悟空 | 242 |
    18. | 曹孟德 | 233 |
    19. | 唐三藏 | 221 |
    20. | 孙权 | 221 |
    21. +-----------+-------+
    22. 5 rows in set (0.01 sec)
    23. mysql> select name ,math+english+chinese total from exam_result where english+math+chinese >200 order by total desc limit 1;
    24. +-----------+-------+
    25. | name | total |
    26. +-----------+-------+
    27. | 猪悟能 | 276 |
    28. +-----------+-------+
    29. 1 row in set (0.00 sec)
    30. mysql> select name ,math+english+chinese total from exam_result where english+math+chinese >200 order by total desc limit 2 offset 0;
    31. +-----------+-------+
    32. | name | total |
    33. +-----------+-------+
    34. | 猪悟能 | 276 |
    35. | 孙悟空 | 242 |
    36. +-----------+-------+
    37. 2 rows in set (0.00 sec)
    38. mysql> select name ,math+english+chinese total from exam_result where english+math+chinese >200 order by total desc limit 2 offset 2;
    39. +-----------+-------+
    40. | name | total |
    41. +-----------+-------+
    42. | 曹孟德 | 233 |
    43. | 唐三藏 | 221 |
    44. +-----------+-------+
    45. 2 rows in set (0.00 sec)
    46. mysql> select name ,math+english+chinese total from exam_result where english+math+chinese >200 order by total desc limit 2 offset 4;
    47. +--------+-------+
    48. | name | total |
    49. +--------+-------+
    50. | 孙权 | 221 |
    51. +--------+-------+
    52. 1 row in set (0.00 sec)
    53. mysql>

    表之“删”操作

    删表

     
    
    1. mysql> create table t1( id int, name varchar(20) );
    2. Query OK, 0 rows affected (0.02 sec)
    3. mysql> show tables;
    4. +--------------+
    5. | Tables_in_d1 |
    6. +--------------+
    7. | exam_result |
    8. | students |
    9. | t1 |
    10. +--------------+
    11. 3 rows in set (0.00 sec)
    12. mysql> drop table if exists t1;
    13. Query OK, 0 rows affected (0.01 sec)
    14. mysql> show tables;
    15. +--------------+
    16. | Tables_in_d1 |
    17. +--------------+
    18. | exam_result |
    19. | students |
    20. +--------------+
    21. 2 rows in set (0.00 sec)
    22. mysql>

    Delete

    删除孙悟空同学的考试成绩

     
    
    1. mysql> select *from exam_result where name='孙悟空';
    2. +----+-----------+---------+------+---------+
    3. | id | name | chinese | math | english |
    4. +----+-----------+---------+------+---------+
    5. | 2 | 孙悟空 | 174 | 80 | 77 |
    6. +----+-----------+---------+------+---------+
    7. 1 row in set (0.00 sec)
    8. mysql> delete from exam_result where name='孙悟空';
    9. Query OK, 1 row affected (0.00 sec)
    10. mysql> select *from exam_result;
    11. +----+-----------+---------+------+---------+
    12. | id | name | chinese | math | english |
    13. +----+-----------+---------+------+---------+
    14. | 1 | 唐三藏 | 134 | 98 | 56 |
    15. | 3 | 猪悟能 | 176 | 98 | 90 |
    16. | 4 | 曹孟德 | 140 | 90 | 67 |
    17. | 5 | 刘玄德 | 110 | 115 | 45 |
    18. | 6 | 孙权 | 140 | 73 | 78 |
    19. | 7 | 宋公明 | 150 | 95 | 30 |
    20. +----+-----------+---------+------+---------+
    21. 6 rows in set (0.00 sec)
    22. mysql> select name ,chinese +math+english total from exam_result ;
    23. +-----------+-------+
    24. | name | total |
    25. +-----------+-------+
    26. | 唐三藏 | 288 |
    27. | 猪悟能 | 364 |
    28. | 曹孟德 | 297 |
    29. | 刘玄德 | 270 |
    30. | 孙权 | 291 |
    31. | 宋公明 | 275 |
    32. +-----------+-------+
    33. 6 rows in set (0.00 sec)
    34. mysql> select name ,chinese +math+english total from exam_result order by total;
    35. +-----------+-------+
    36. | name | total |
    37. +-----------+-------+
    38. | 刘玄德 | 270 |
    39. | 宋公明 | 275 |
    40. | 唐三藏 | 288 |
    41. | 孙权 | 291 |
    42. | 曹孟德 | 297 |
    43. | 猪悟能 | 364 |
    44. +-----------+-------+
    45. 6 rows in set (0.00 sec)
    46. mysql> select name ,chinese +math+english total from exam_result order by total desc;
    47. +-----------+-------+
    48. | name | total |
    49. +-----------+-------+
    50. | 猪悟能 | 364 |
    51. | 曹孟德 | 297 |
    52. | 孙权 | 291 |
    53. | 唐三藏 | 288 |
    54. | 宋公明 | 275 |
    55. | 刘玄德 | 270 |
    56. +-----------+-------+
    57. 6 rows in set (0.01 sec)
    58. mysql> select name ,chinese +math+english total from exam_result order by total asc;
    59. +-----------+-------+
    60. | name | total |
    61. +-----------+-------+
    62. | 刘玄德 | 270 |
    63. | 宋公明 | 275 |
    64. | 唐三藏 | 288 |
    65. | 孙权 | 291 |
    66. | 曹孟德 | 297 |
    67. | 猪悟能 | 364 |
    68. +-----------+-------+
    69. 6 rows in set (0.00 sec)
    70. mysql> select name ,chinese +math+english total from exam_result order by total asc limit 1;
    71. +-----------+-------+
    72. | name | total |
    73. +-----------+-------+
    74. | 刘玄德 | 270 |
    75. +-----------+-------+
    76. 1 row in set (0.00 sec)
    77. mysql> delete from exam_result order by english +math+chinese asc limit 1;
    78. Query OK, 1 row affected (0.00 sec)
    79. mysql> select name ,chinese+math+english total from exam_result order by total asc limit 1;
    80. +-----------+-------+
    81. | name | total |
    82. +-----------+-------+
    83. | 宋公明 | 275 |
    84. +-----------+-------+
    85. 1 row in set (0.00 sec)
    86. mysql> select * from exam_result;
    87. +----+-----------+---------+------+---------+
    88. | id | name | chinese | math | english |
    89. +----+-----------+---------+------+---------+
    90. | 1 | 唐三藏 | 134 | 98 | 56 |
    91. | 3 | 猪悟能 | 176 | 98 | 90 |
    92. | 4 | 曹孟德 | 140 | 90 | 67 |
    93. | 6 | 孙权 | 140 | 73 | 78 |
    94. | 7 | 宋公明 | 150 | 95 | 30 |
    95. +----+-----------+---------+------+---------+
    96. 5 rows in set (0.00 sec)
    97. mysql>

    删除整个表

     
    
    1. mysql> create table for_delete(
    2. -> id int primary key auto_increment,
    3. -> name varchar(20)
    4. -> );
    5. Query OK, 0 rows affected (0.02 sec)
    6. mysql> insert into for_delete (name) value ('A'),('B'),('C');
    7. Query OK, 3 rows affected (0.01 sec)
    8. Records: 3 Duplicates: 0 Warnings: 0
    9. mysql> show CREATE table for_delete\G
    10. *************************** 1. row ***************************
    11. Table: for_delete
    12. Create Table: CREATE TABLE `for_delete` (
    13. `id` int(11) NOT NULL AUTO_INCREMENT,
    14. `name` varchar(20) DEFAULT NULL,
    15. PRIMARY KEY (`id`)
    16. ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
    17. 1 row in set (0.02 sec)
    18. mysql> delete from for_delete;
    19. Query OK, 3 rows affected (0.01 sec)
    20. mysql> select *from for_delete;
    21. Empty set (0.00 sec)
    22. mysql> show create table for_delete\G
    23. *************************** 1. row ***************************
    24. Table: for_delete
    25. Create Table: CREATE TABLE `for_delete` (
    26. `id` int(11) NOT NULL AUTO_INCREMENT,
    27. `name` varchar(20) DEFAULT NULL,
    28. PRIMARY KEY (`id`)
    29. ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
    30. 1 row in set (0.00 sec)
    31. mysql> insert into for_delete (name) value ('E');
    32. Query OK, 1 row affected (0.00 sec)
    33. mysql> select * from for_delete;
    34. +----+------+
    35. | id | name |
    36. +----+------+
    37. | 4 | E |
    38. +----+------+
    39. 1 row in set (0.00 sec)
    40. mysql>

    截断表

    无法回滚,会重置auto_increment项。

     
    
    1. mysql> create table for_truncate(
    2. -> id int primary key auto_increment,
    3. -> name varchar(20)
    4. -> );
    5. Query OK, 0 rows affected (0.04 sec)
    6. mysql> desc for_truncate;
    7. +-------+-------------+------+-----+---------+----------------+
    8. | Field | Type | Null | Key | Default | Extra |
    9. +-------+-------------+------+-----+---------+----------------+
    10. | id | int(11) | NO | PRI | NULL | auto_increment |
    11. | name | varchar(20) | YES | | NULL | |
    12. +-------+-------------+------+-----+---------+----------------+
    13. 2 rows in set (0.00 sec)
    14. mysql> insert into for_truncate (name) value('A'),('B'),('C');
    15. Query OK, 3 rows affected (0.01 sec)
    16. Records: 3 Duplicates: 0 Warnings: 0
    17. mysql> select * from for_truncate;
    18. +----+------+
    19. | id | name |
    20. +----+------+
    21. | 1 | A |
    22. | 2 | B |
    23. | 3 | C |
    24. +----+------+
    25. 3 rows in set (0.00 sec)
    26. mysql> show create table for_truncate\G
    27. *************************** 1. row ***************************
    28. Table: for_truncate
    29. Create Table: CREATE TABLE `for_truncate` (
    30. `id` int(11) NOT NULL AUTO_INCREMENT,
    31. `name` varchar(20) DEFAULT NULL,
    32. PRIMARY KEY (`id`)
    33. ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
    34. 1 row in set (0.00 sec)
    35. mysql> truncate for_truncate;
    36. Query OK, 0 rows affected (0.02 sec)
    37. mysql> show tables;
    38. +--------------+
    39. | Tables_in_d1 |
    40. +--------------+
    41. | exam_result |
    42. | for_delete |
    43. | for_truncate |
    44. | students |
    45. | test_null |
    46. +--------------+
    47. 5 rows in set (0.00 sec)
    48. mysql> select * from for_truncate;
    49. Empty set (0.00 sec)
    50. mysql> show create table for_truncate\G
    51. *************************** 1. row ***************************
    52. Table: for_truncate
    53. Create Table: CREATE TABLE `for_truncate` (
    54. `id` int(11) NOT NULL AUTO_INCREMENT,
    55. `name` varchar(20) DEFAULT NULL,
    56. PRIMARY KEY (`id`)
    57. ) ENGINE=InnoDB DEFAULT CHARSET=utf8
    58. 1 row in set (0.00 sec)
    59. mysql> insert into for_truncate (name) values ('E');
    60. Query OK, 1 row affected (0.01 sec)
    61. mysql> select *from for_truncate;
    62. +----+------+
    63. | id | name |
    64. +----+------+
    65. | 1 | E |
    66. +----+------+
    67. 1 row in set (0.01 sec)
    68. mysql> show create table for_truncate\G
    69. *************************** 1. row ***************************
    70. Table: for_truncate
    71. Create Table: CREATE TABLE `for_truncate` (
    72. `id` int(11) NOT NULL AUTO_INCREMENT,
    73. `name` varchar(20) DEFAULT NULL,
    74. PRIMARY KEY (`id`)
    75. ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
    76. 1 row in set (0.00 sec)
    77. mysql>

    结尾

    最后,感谢您阅读我的文章,希望这些内容能够对您有所启发和帮助。如果您有任何问题或想要分享您的观点,请随时在评论区留言。

    同时,不要忘记订阅我的博客以获取更多有趣的内容。在未来的文章中,我将继续探讨这个话题的不同方面,为您呈现更多深度和见解。

    谢谢您的支持,期待与您在下一篇文章中再次相遇!

  • 相关阅读:
    PCB layout有DRC为什么还要用CAM和DFM检查?
    echarts文档解读
    centos启动停留在started GNOME display manager
    Pandas时序数据Time Series
    ram和rom的区别
    Python函数
    Java volatile功能简介说明
    动手学深度学习(pytorch)2
    什么是单链表?
    如何通过7个步骤编写出色的在线用户手册
  • 原文地址:https://blog.csdn.net/m0_74163972/article/details/136310628