mysql中的union和order by、limit
我有一个表
CREATE TABLE `test1` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`desc` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
(1)以下查询会报错误:[Err] 1221 - Incorrect usage of UNION and ORDER BY
select * from test1 where name like 'A%' order by name
union
select * from test1 where name like 'B%' order by name
应改为:
select * from test1 where name like 'A%'
union
select * from test1 where name like 'B%' order by name
因为union中,在不用括号的情况下,只能用一个order by(想一想,如果union两边的order by的列名不一样会怎么样),这会对union后的结果集进行排序
或者改为:
(select * from test1 where name like 'A%' order by name)
union
(select * from test1 where name like 'B%' order by name)
这两个order by在union前进行
(2)同样的
select * from test1 where name like 'A%' limit 10
union
select * from test1 where name like 'B%' limit 20
相当于
(select * from test1 where name like 'A%' limit 10)
union
(select * from test1 where name like 'B%') limit 20
即后一个limit作用于的是union后的结果集,而不是union后的select
也可以加括号来得到你想要的结果
(select * from test1 where name like 'A%' limit 10)
union
(select * from test1 where name like 'B%' limit 20)
(3)UNION和UNION ALL区别
union会过滤掉union两边的select得到的结果集中的重复的行,而union all不会过滤掉重复的行
相关文档:
数据库特点
--使用partition,存在上百个分区
--建表时指定了data_dir和index_dir,数据不是存储在默认位置,而是在mysqld的数据目录下link到真正的数据文件
备份恢复要求
--备份出来的数据恢复时要恢复成不同的表名
--恢复出来的数据实际存储位置也要存储在与原表不同的位置
问题
如果直接mysqldump-sourc ......
我的系统是ubuntu6.06,最近新装好的mysql在进入mysql工具时,总是有错误提示:
# mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
使用网上介绍的方法修改root用户的密码:
# mysqladmin -uroot -p password 'newpassword'
Enter pass ......
1.select max(id) from user;
2.select last_insert_id() as id from user limit 1;
(这个测试的返回id一直是0,有点问题)
3.储存过程
1)
oracel中
create sequence seqID
minvalue 1
maxvalue 999999999999999999999999999
start with 1
increment by 1
nocache
order;
create or replace procedure ......
对mysql的优化不在行,搞过几次优化,但是都不是很理想,还是浪费资源太多。一直发现我的mysql的缓存命中率极差,情况良好的时候到达过60-70%,但是运行时间一长,只有10-20%。查了一些资料,关于缓存的一些参数记录
mysql> SHOW VARIABLES LIKE ‘%query_cache%’;
+—————&m ......