数据库复习之第四天(2019526)

    xiaoxiao2024-10-29  85

    1.Mysql 连接的使用 在这里介绍的是两张表或多张表的查询、更新、删除操作;使用 Mysql 的 JOIN 来联合多表查询。 JOIN 按照功能大致分为如下三类:

    INNER JOIN(内连接,或等值连接):获取两个表中字段匹配关系的记录。

    LEFT JOIN(左连接):获取左表所有记录,即使右表没有对应匹配的记录。

    RIGHT JOIN(右连接): 与 LEFT JOIN相反,用于获取右表所有记录,即使左表没有对应匹配的记录 (1)内连接(inner join) 相当于求两个集合的交集。 例子: select s.id,s.name sname,s.age sage,s.grader sgrader,t.id,t.name tname,t.age tage from students s inner join tearch t on s.id = t.id; (2)左连接(left join) MySQL LEFT JOIN 会读取左边数据表的全部数据,即便右边表无对应数据. 例子:

    #左连接(left join) select s.id,s.name,s.age,s.grader ,t.id,t.name,t.age from students s left join tearch t on s.id=t.id; (3)右连接(right join) MySQL RIGHT JOIN 会读取右边数据表的全部数据,即便左边边表无对应数据。 例子:

    #右连接(right join) select s.id,s.name,s.age,s.grader ,t.id,t.name,t.age from students s right join tearch t on s.id=t.id; 2.MySQL NULL 值处理 我们已经知道 MySQL 使用 SQL SELECT 命令及 WHERE 子句来读取数据表中的数据,但是当提供的查询条件字段为 NULL 时,该命令可能就无法正常工作。为了解决这种情况,MySQL提供了三大运算符:

    IS NULL: 当列的值是 NULL,此运算符返回 true。

    IS NOT NULL: 当列的值不为 NULL, 运算符返回 true。

    <=>: 比较操作符(不同于=运算符),当比较的的两个值为 NULL 时返回 true。 例子:

    select * from students where age is not null;

    select * from students where age is null; 3.MySQL 正则表达式 例子:

    select * from students where name regexp ‘^e’;# ^表示以e作为开头的字符串的数据 select * from students where name regexp ‘e$’;# $表示以e作为结尾的字符串的数据 select * from students where name regexp ‘e’;#查找name字段中包含’e’字符串的所有数据; 正则表达式可以去上网学学…

    最新回复(0)