sql>select*from lab1.person LIMIT 0, 1001; +------+-----+ | NAME | AGE | +------+-----+ | A |10| | B |20| | C |30| | D |40| | E |50| | F |60| | G |70| | H |80| | I |90| | J |100| | K |110| | L |110| | M |110| | L |110| | M |110| | L |110| | M |110| +------+-----+
1 2 3 4 5 6 7
sql>-- 方法一,直接使用DISTINCT关键字 SELECTCOUNT(DISTINCT NAME, AGE) FROM PERSON LIMIT 0, 1001; +---------------------------+ |COUNT(DISTINCT NAME, AGE) | +---------------------------+ |13| +---------------------------+
1 2 3 4 5 6 7 8 9 10 11 12
sql>-- 方法二,使用子查询和GROUP BY SELECTCOUNT(*) FROM ( SELECT NAME, AGE FROM PERSON GROUPBY NAME, AGE ) AS UniqueTuples LIMIT 0, 1001; +----------+ |COUNT(*) | +----------+ |13| +----------+
1 2 3 4 5 6 7
sql>-- 方法三,使用GROUP BY和HAVING SELECTCOUNT(*) FROM (SELECTDISTINCT NAME, AGE FROM PERSON) AS UniqueTuples LIMIT 0, 1001; +----------+ |COUNT(*) | +----------+ |13| +----------+
作业2
实现一trigger,使得在向department添加行时复制内容到student3表中,代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
DROPTRIGGER IF EXISTS copy_to_std3; DELIMITER $$
CREATETRIGGER copy_to_std3 AFTER INSERT ON department FOREACHROW BEGIN DECLARE DL varchar(10); DECLARE DN varchar(12);
SET DL = NEW.department_leader; SET DN = NEW.department_name;