单表更新
UPDATE 表格 SET 列 = 更新点;
给年龄统一增加五岁
mysql> select * from user;+----+----------+----------------------------------+-----+-----+-----+------+--------+| id | username | pwd | sex | age | edu | city | com |+----+----------+----------------------------------+-----+-----+-----+------+--------+| 1 | 小二 | e10adc3949ba59abbe56e057f20f883e | 0 | 18 | 0 | 深圳 | 小公司 || 2 | 张三 | e10adc3949ba59abbe56e057f20f883e | 0 | 18 | 0 | 深圳 | 小公司 || 3 | 李四 | e10adc3949ba59abbe56e057f20f883e | 0 | 18 | 0 | 深圳 | 大公司 || 4 | 王五 | e10adc3949ba59abbe56e057f20f883e | 0 | 18 | 0 | 北京 | 小公司 || 5 | 美美 | 508df4cb2f4d8f80519256258cfb975f | 1 | 20 | 3 | 上海 | 国企 |+----+----------+----------------------------------+-----+-----+-----+------+--------+5 rows in set (0.04 sec)mysql> UPDATE user SET age = age + 5;Query OK, 5 rows affected (0.01 sec)Rows matched: 5 Changed: 5 Warnings: 0mysql> select * from user;+----+----------+----------------------------------+-----+-----+-----+------+--------+| id | username | pwd | sex | age | edu | city | com |+----+----------+----------------------------------+-----+-----+-----+------+--------+| 1 | 小二 | e10adc3949ba59abbe56e057f20f883e | 0 | 23 | 0 | 深圳 | 小公司 || 2 | 张三 | e10adc3949ba59abbe56e057f20f883e | 0 | 23 | 0 | 深圳 | 小公司 || 3 | 李四 | e10adc3949ba59abbe56e057f20f883e | 0 | 23 | 0 | 深圳 | 大公司 || 4 | 王五 | e10adc3949ba59abbe56e057f20f883e | 0 | 23 | 0 | 北京 | 小公司 || 5 | 美美 | 508df4cb2f4d8f80519256258cfb975f | 1 | 25 | 3 | 上海 | 国企 |+----+----------+----------------------------------+-----+-----+-----+------+--------+5 rows in set (0.00 sec)
还可以更改多行
mysql> UPDATE user SET age = age - id,sex = 0;Query OK, 5 rows affected (0.01 sec)Rows matched: 5 Changed: 5 Warnings: 0mysql> select * from user;+----+----------+----------------------------------+-----+-----+-----+------+--------+| id | username | pwd | sex | age | edu | city | com |+----+----------+----------------------------------+-----+-----+-----+------+--------+| 1 | 小二 | e10adc3949ba59abbe56e057f20f883e | 0 | 22 | 0 | 深圳 | 小公司 || 2 | 张三 | e10adc3949ba59abbe56e057f20f883e | 0 | 21 | 0 | 深圳 | 小公司 || 3 | 李四 | e10adc3949ba59abbe56e057f20f883e | 0 | 20 | 0 | 深圳 | 大公司 || 4 | 王五 | e10adc3949ba59abbe56e057f20f883e | 0 | 19 | 0 | 北京 | 小公司 || 5 | 美美 | 508df4cb2f4d8f80519256258cfb975f | 0 | 20 | 3 | 上海 | 国企 |+----+----------+----------------------------------+-----+-----+-----+------+--------+5 rows in set (0.00 sec)
还可以增加条件,需要在表达式后面添加:where 条件
给偶数位的年龄增加十岁
mysql> UPDATE user SET age = age + 10 WHERE id % 2 = 0;Query OK, 2 rows affected (0.01 sec)Rows matched: 2 Changed: 2 Warnings: 0mysql> select * from user;+----+----------+----------------------------------+-----+-----+-----+------+--------+| id | username | pwd | sex | age | edu | city | com |+----+----------+----------------------------------+-----+-----+-----+------+--------+| 1 | 小二 | e10adc3949ba59abbe56e057f20f883e | 0 | 22 | 0 | 深圳 | 小公司 || 2 | 张三 | e10adc3949ba59abbe56e057f20f883e | 0 | 31 | 0 | 深圳 | 小公司 || 3 | 李四 | e10adc3949ba59abbe56e057f20f883e | 0 | 20 | 0 | 深圳 | 大公司 || 4 | 王五 | e10adc3949ba59abbe56e057f20f883e | 0 | 29 | 0 | 北京 | 小公司 || 5 | 美美 | 508df4cb2f4d8f80519256258cfb975f | 0 | 20 | 3 | 上海 | 国企 |+----+----------+----------------------------------+-----+-----+-----+------+--------+5 rows in set (0.00 sec)
单表删除
DELETE FROM 表名 WHERE 条件
删除张三 id=2
mysql> DELETE FROM user WHERE id=2;Query OK, 1 row affected (0.01 sec)mysql> select * from user;+----+----------+----------------------------------+-----+-----+-----+------+--------+| id | username | pwd | sex | age | edu | city | com |+----+----------+----------------------------------+-----+-----+-----+------+--------+| 1 | 小二 | e10adc3949ba59abbe56e057f20f883e | 0 | 22 | 0 | 深圳 | 小公司 || 3 | 李四 | e10adc3949ba59abbe56e057f20f883e | 0 | 20 | 0 | 深圳 | 大公司 || 4 | 王五 | e10adc3949ba59abbe56e057f20f883e | 0 | 29 | 0 | 北京 | 小公司 || 5 | 美美 | 508df4cb2f4d8f80519256258cfb975f | 0 | 20 | 3 | 上海 | 国企 |+----+----------+----------------------------------+-----+-----+-----+------+--------+4 rows in set (0.00 sec)
解析select
查询表达式
每一个表达式表示想要的一列,必须有至少一个
多个列之间以英文都好分隔
星号表示所有列,表明.*表示该表明的所有列
查询表达式【AS】为其赋值别名
别名可用于GROUP BY,ORDER BY或HAVING子句
WHERE条件表达式
对记录进行过滤,如果没有指定where子句,则显示所有记录
在where表达式中,可以使用MySQL支持的函数或者运算符
查询结果分组
GROUP BY 列名
按照edu分组
mysql> SELECT edu FROM user GROUP BY edu;+-----+| edu |+-----+| 0 || 3 |+-----+
按照com分组
mysql> SELECT com FROM user GROUP BY com;+--------+| com |+--------+| 小公司 || 大公司 || 国企 |+--------+3 rows in set (0.00 sec)
设置分组条件
SELECT * FROM 表格 HAVING 条件
按照年龄分组
mysql> SELECT age FROM user GROUP BY age HAVING count(id)>1;+-----+| age |+-----+| 20 |+-----+1 row in set (0.00 sec)
查找偶数字段
mysql> SELECT * FROM user HAVING id % 2 = 0;+----+----------+----------------------------------+-----+-----+-----+------+--------+| id | username | pwd | sex | age | edu | city | com |+----+----------+----------------------------------+-----+-----+-----+------+--------+| 4 | 王五 | e10adc3949ba59abbe56e057f20f883e | 0 | 29 | 0 | 北京 | 小公司 |+----+----------+----------------------------------+-----+-----+-----+------+--------+1 row in set (0.00 sec)
对查询结果进行排序
正序 ASC
倒序 DESC
按照id倒叙抽查询
mysql> SELECT * FROM user ORDER BY id DESC;+----+----------+----------------------------------+-----+-----+-----+------+--------+| id | username | pwd | sex | age | edu | city | com |+----+----------+----------------------------------+-----+-----+-----+------+--------+| 5 | 美美 | 508df4cb2f4d8f80519256258cfb975f | 0 | 20 | 3 | 上海 | 国企 || 4 | 王五 | e10adc3949ba59abbe56e057f20f883e | 0 | 29 | 0 | 北京 | 小公司 || 3 | 李四 | e10adc3949ba59abbe56e057f20f883e | 0 | 20 | 0 | 深圳 | 大公司 || 1 | 小二 | e10adc3949ba59abbe56e057f20f883e | 0 | 22 | 0 | 深圳 | 小公司 |+----+----------+----------------------------------+-----+-----+-----+------+--------+4 rows in set (0.00 sec)
按照age正序排序
mysql> SELECT * FROM user ORDER BY age ASC;+----+----------+----------------------------------+-----+-----+-----+------+--------+| id | username | pwd | sex | age | edu | city | com |+----+----------+----------------------------------+-----+-----+-----+------+--------+| 3 | 李四 | e10adc3949ba59abbe56e057f20f883e | 0 | 20 | 0 | 深圳 | 大公司 || 5 | 美美 | 508df4cb2f4d8f80519256258cfb975f | 0 | 20 | 3 | 上海 | 国企 || 1 | 小二 | e10adc3949ba59abbe56e057f20f883e | 0 | 22 | 0 | 深圳 | 小公司 || 4 | 王五 | e10adc3949ba59abbe56e057f20f883e | 0 | 29 | 0 | 北京 | 小公司 |+----+----------+----------------------------------+-----+-----+-----+------+--------+4 rows in set (0.00 sec)
按照age正序,id倒叙排序
mysql> SELECT * FROM user ORDER BY age ASC,id DESC;+----+----------+----------------------------------+-----+-----+-----+------+--------+| id | username | pwd | sex | age | edu | city | com |+----+----------+----------------------------------+-----+-----+-----+------+--------+| 5 | 美美 | 508df4cb2f4d8f80519256258cfb975f | 0 | 20 | 3 | 上海 | 国企 || 3 | 李四 | e10adc3949ba59abbe56e057f20f883e | 0 | 20 | 0 | 深圳 | 大公司 || 1 | 小二 | e10adc3949ba59abbe56e057f20f883e | 0 | 22 | 0 | 深圳 | 小公司 || 4 | 王五 | e10adc3949ba59abbe56e057f20f883e | 0 | 29 | 0 | 北京 | 小公司 |+----+----------+----------------------------------+-----+-----+-----+------+--------+4 rows in set (0.00 sec)
文章转载自小二测试成长,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




