赞
踩
create table new_table select * from old_table
create table new_table like old_table insert new_table select * from old_table
这两种方法的特点:第一种方法需要手动添加主键、索引等。第二种方法是完全复制,推荐这种方法。
此外,如果想从另外表导入数据:
Insert into table1(col1,col2,…) Select col1, col2,… from table2
分页在实际的项目中应用的十分广泛, 但是当数据量大时, 其效率问题令人担忧。先看下我们通常采用的分页语句:
Select * from table where …. Order by X limit start, size
Select * from table where …. Order by X limit 10000, 10
随着start的增大,查询的效率越差, 需要进一步优化。
优化的方式主要有两种方式 1:子查询 2:连接查询
Select * from (Select * from table where id>(select id from table order by id desc limit 10000, 1) limit 10) order by id desc
SELECT * FROM table INNER JOIN ( SELECT id FROM table ORDER BY id DESC LIMIT 10000,10) t2 USING (id)
用存储过程实现计算某用户的排名
CREATE PROCEDURE `calc_ranks `(username varchar(50)
BEGIN
SET @username = username;
PREPARE STMT FRO
'select CAST(ranking.rank AS SIGNED)
from (
select @x := @x + 1 as rank, rd.username, rd.total
from ( select a.username, a.total
from table1 a left join table2 b on a.username=b.user_name
where b.is_active = 1 and b.is_enabled = 1
order by a.total desc, b.nick_name asc
) rd, (select @x := 0) r
) ranking
where ranking.username = ?';
EXECUTE STMT USING @username;
END
导出整个数据库
导出一个表
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。