数据库相关操作

428 阅读3分钟

查询语句 select

SELECT column(列名), another_column, … FROM table(表名);

  • 查询所有列 select * from table;
  • 查询指定列 select name, sex from table;
  • 根据条件查询 select * from table where id < 5;
  • 查询总数 select count(*) from table;
  • 指定一个具体的值为一个字段 select name, sex, 19 as age from table; 查询出来将多一age列值为19
  • 修改列的名字 使用as关键字 select name as cname, sex from table;
  • 列去重DISTINCT select DISTINCT name, sex from table; 会将name重复的行删除

条件查询 where

SELECT column(列名) FROM table(表名) where conditions(条件); 当条件值为字符串时需要使用''包裹,否则将会被识别为列;

  • 比较符 =(等于) !=(不等于) <(小于) <=(小于等于) >(大于) >=(大于等于)
  • 使用and来关联多个条件 select * from table where id < 5 and sex = 'man'; and表示需要同时满足多个条件
  • 使用or来关联多个条件 select * from table where id < 5 and sex = 'man'; or表示满足其中一个条件即可
  • 如果有多个查询条件其中一个条件是满足两个条件之一的or的关系,需要将这个or的条件加上()
  • 使用between and 表示区间条件 select * from table where id between 1 and 10; id在1和10之间的
  • 使用 not between and 表示区间条件 select * from table where id not between 1 and 10; id不在1和10之间的
  • 使用 in 表示在某一个指定列表内 select * from table where id in (1, 2, 3); id是1,2,3其中之一的
  • 使用 not in 表示不在某一个指定列表内 select * from table where id not in (1, 2, 3); id不是1,2,3其中之一的
  • like操作符 select * from table where name like 'dyx'; name字段可以匹配到dyx(严格匹配)
  • not like操作符 select * from table where name not like 'dyx'; name字段不可以匹配到dyx
  • % 操作符表示任意字符,可以在字符串前后任意位置使用 select * from table where name like '%dyx%'; 表示name字段可以匹配到的范围是dyx前后可以有任意字符(模糊匹配)
  • _ 操作符 select * from table where name like 'dyx_'; 表示name字段可以匹配到的范围是dyx后有任意一个字符,但是匹配不到dyx

排序 ORDER BY

SELECT column(列名) FROM table(表名) where conditions(条件) ORDER BY column ASC/DESC, another_column ASC/DESC;

  • ASC 是升序, DESC是降序(2020, 2019)

限制返回条数 LIMIT

LIMIT可以来指定只返回多少行结果; SELECT column(列名) FROM table(表名) where conditions(条件) limit 10; 最多返回10条

指定数据选择时的偏移量 offset

SELECT column(列名) FROM table(表名) where conditions(条件) offset 10; 从第十一条开始返回

  • offset和limit同用时需要先使用limit 再使用offset

连表查询

有时我们需要从多张表中获取数据,可以用两张表中的唯一的字段进行关联获取理想数据,其余的where order等条件是在连表后的基础上进行的。使用on 关键字来定义关联条件。

INNER JOIN

取交集,两张表中通过关联关系匹配不到的数据将会被舍弃。连接后的新表包含两张表中的所有字段。

SELECT * FROM oneTable (主表)
  INNER JOIN twoTable (要连接的表)
  ON oneTable.id = twoTable.id
  where ...;

OUTER JOIN

使用语法同INNER JOIN,替换JOIN关键字即可。将两个表数据连接,保留A或B的原有行,如果某一行在另一个表不存在,会用 NULL来填充结果数据。

  • LEFT JOIN

使用表A连接表B, LEFT JOIN保留A的所有行,不管有没有能匹配上。

  • RIGHT JOIN

使用表A连接表B, RIGHT JOIN保留B的所有行,不管有没有能匹配上。

  • FULL JOIN

使用表A连接表B, FULL JOIN会同时保留A、B的所有行,不管有没有能匹配上。

对NULL值的判断

  • IS NULL 判断是否为NULL
  • IS NOT NULL 判断是否不是NULL
SELECT column, another_column, … FROM mytable WHERE column IS NULL;

as 关键字

使用as关键字可以为column(列)和table(表)定义一个别名

使用表达式对sql语句进行一些逻辑计算

  • 可以对列进行一些数学计算然后as一个别名返回
// 将score_one列和score_two列的数据相加并重命名为score_total列返回
select id, (score_one + score_two) as score_total from table;

// 将score_one列和score_two列的数据相加并除以score_num字段重命名为score_value列返回
select id, (score_one + score_two) / score_num as score_value from table;

// 将score_one列的10倍值并重命名为score_ten列返回
select id, (score_one * 10) as score_ten from table;
  • where条件中也可以做一些数学运算
// 匹配所有year为偶数的数据
select id, name, year from table where year % 2 = 0;
  • 使用length(column)可以计算某字段的长度
// 取title字段长度最长的前三个
select length(title) as title_len,title,from movies order by title_len desc limit 3;

相关操作

  • 创建表id自增,自动同步创建时间和更新时间的表
CREATE TABLE user(
  `id` INT NOT NULL AUTO_INCREMENT COMMENT '主键id', 
  `create_date` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_date`  datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  `name` VARCHAR(100) NOT NULL COMMENT '用户名',
  `password` VARCHAR(100) NOT NULL COMMENT '密码',
  `permission` VARCHAR(100) NOT NULL COMMENT '用户权限',
  PRIMARY KEY ( `id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
  • 连表进行数据订正
UPDATE table_a as A
INNER JOIN table_b B ON A.id = B.id
SET A.name = B.name
WHERE B.status = 'RUNNING'
  • 登录数据库 mysql -u root -p
  • 使用数据库 use database(数据库名);
  • 显示所有表 show tables;
  • 删除数据 delete from table(表名) where conditions(条件);
  • 更新数据 update table(表名) set cloumn(列名1) = value(列值1), column(列名2) = value(列值2) where conditions(条件);
  • 查询表的所有列 show columns from table(表名);