虚拟表,和普通表一样使用
create view 视图名
as
查询语句;
方法①:create or replace view 视图名 as 查询语句;
方法②:alter view 视图名 as 查询语句;
drop view 视图名,视图名,…
DESC 视图名;–查看视图相关字段
SHOW CERATE VIEW 视图名;–查看视图相关语句
SELECT
s.*,
(CASE WHEN t1.cid = '01' THEN t1.score END ) 语文,
(CASE WHEN t2.cid = '02' THEN t2.score END ) 数学
FROM
t_mysql_student s,
( SELECT * FROM t_mysql_score WHERE cid = '01' ) t1,
( SELECT * FROM t_mysql_score WHERE cid = '02' ) t2
WHERE
s.sid = t1.sid
AND t1.sid = t2.sid
AND t1.score > t2.score
SELECT
s.*,
(CASE WHEN t1.cid = '01' THEN t1.score END ) 语文,
(CASE WHEN t2.cid = '02' THEN t2.score END ) 数学
FROM
t_mysql_student s,
( SELECT * FROM t_mysql_score WHERE cid = '01' ) t1,
( SELECT * FROM t_mysql_score WHERE cid = '02' ) t2
WHERE
s.sid = t1.sid
AND t1.sid=t2.sid
SELECT
s.*,
( CASE WHEN t1.cid = '01' THEN t1.score END ) 语文,
( CASE WHEN t2.cid = '02' THEN t2.score END ) 数学
FROM
t_mysql_student s
INNER JOIN
( SELECT * FROM t_mysql_score WHERE cid = '01' ) t1 ON s.sid = t1.sid
LEFT JOIN
( SELECT * FROM t_mysql_score WHERE cid = '02' ) t2 ON t1.sid = t2.sid;
select
s.*,
(case when sc.cid='01' then sc.score end) 语文,
(case when sc.cid='02' then sc.score end) 数学
from
t_mysql_student s,
t_mysql_score sc
where
s.sid=sc.sid
and
s.sid not in
(select sid from t_mysql_score where cid='01')
and sc.cid='02';
select
s.sid,
s.sname,
avg(sc.score) n
from
t_mysql_student s left join
t_mysql_score sc on s.sid = sc.sid
GROUP BY s.sid,s.sname
HAVING n>=60
select
s.*
from
t_mysql_student s INNER JOIN
t_mysql_score sc on s.sid = sc.sid
GROUP BY s.sid,s.sname
select
s.sid,
s.sname,
count(sc.score) 选课总数,
sum(sc.score) 总成绩
from
t_mysql_student s LEFT JOIN
t_mysql_score sc on s.sid = sc.sid
GROUP BY s.sid,s.sname
索引是一种特殊的数据库结构,由数据表中的一列或多列组合而成,可以用来快速查询数据表中有某一特定值的记录
使用索引可以很大程度上提高数据库的查询速度,还有效的提高了数据库系统的性能。
CREATE TABLEt ‘Iog’(
‘id’ varchar(32) NOT NULL COMMENT唯一标识
‘ip’ varchar(15) NOT NULL COMMENT ‘IP地址’,
‘userid’ varchar(32) NOT NULL COMMENT ‘用户ID’,
‘moduleid’ varchar(32) NOT NULL COMMENT ‘模块ID’,
‘content’ varchar(500) NOT NULL COMMENT ‘日志内容’,
‘createdate’ timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT ‘创建日期’,
‘url’ varchar(100) DEFAULT NULL COMMENT ‘请求URL地’,
PRIMARY KEY (‘id’)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
–1)普通索引:是最基本的索引,它没有任何限制;
–0.762s
select * from t_log;
– 建索引前0.12s
select * from t log where moduleid =10040199’;
– 创建索引所花费的时间:1.593s
Create index idx_moduleid on t_log(moduleid);
– 建索引前 0.001s
select from t_log where moduleid =10040199’;
– 可以查看走过的索引
EXPLAIN select * from t_log where moduleid =‘10040199’;
2)唯一索引:与前面的普通索引类似,不同的就是:索引列的值必须唯一,但允许有空值。如果是组合索引,则列值的组合必须唯一;
– Duplicate entry ‘/quartz/queryJobLst’ for key ‘idx_ur’ 有重复列段
create UNIQUE index idx_url on t_log(url);
drop index idx_url on t_log;
– 3)主键索引:是一种特殊的唯一索引,一个表只能有一个主键,不允许有空值;
– 主键索引所花费的时间: 0s
select * from t_log where id =‘07489cdafd6d4a3489884cd3c00c7b27’;
EXPLAIN select * from t log where id =07489cdafd6d4a3489884cd3c00c7b27’
– 4)组合索引:指多个字段上创建的索引,只有在查询条件中使用了创建索引时的第一个字段,索引才会被使用。使用组合索引时道循量左前缀集合;
– 花费的时间:3.959s
create index idx_userid_moduleid_url on t_log(userid,moduleid,url);
– 走组合索引
EXPLAIN select * from t_log where userid = " and moduleid = " and url = ";
EXPLAIN select * from t_log where userid = " and moduleid = ";
EXPLAIN select * from t_log where userid = ";
EXPLAIN select * from t_log where userid = " and url = ";
– 不走组合索引
EXPLAIN select * from t_log where moduleid = ";
EXPLAIN select * from t log where url = ";
EXPLAIN select * from t_log where moduleid = " and url = ";
4.创建索引
CREATE[UNIQUE]FULLTEXT]INDEX 索引名 ON 表名(字段名[(长度][ASCIDESCJ)