易截截图软件、单文件、免安装、纯绿色、仅160KB

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不会过滤掉重复的行


相关文档:

Java+Mysql的数据库查找实现


public class select {
 public List XiuGai_select(String keyword){
  List list=new ArrayList();
        Connection conn = null;
  Statement stmt = null;
  String sql=null;
  ResultSet res = null;
  get ......

Mysql 复制表及结构

一、CREATE TABLE 方法
整表复制 # create table 新表 select * from 旧表;
结构复制 # create table 新表 select * from 旧表 where 1<>1;
二、INSERT INTO 方法
得到建表语句 # show create table 旧表;
新建表
复制数据到新表 INSERT INTO new_table(col1,col2,...) (SELECT col1,col2,... from old_table); ......

MySQL命令

1. 连接mysql:
mysql -h 主机地址 -u 用户名 -p
2.退出mysql:exit
3. 修改密码:
mysqlbinmysqladmin -uroot -p(oldpassword) password newpassword
4.增加用户:
添加一个用户test1 密码为ABC;让他可以在任何主机上登录,并对所有数据库有查询、插入、修改、删除的权限。首先用以root用户连入
mysql,然后键入以下命 ......

mysql缓存参数

对mysql的优化不在行,搞过几次优化,但是都不是很理想,还是浪费资源太多。一直发现我的mysql的缓存命中率极差,情况良好的时候到达过60-70%,但是运行时间一长,只有10-20%。查了一些资料,关于缓存的一些参数记录
mysql> SHOW VARIABLES LIKE ‘%query_cache%’;
+—————&m ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号