MYSQL之union和order by分析([Err] 1221 - Incorrect usage of UNION and ORDER BY)
2017-08-15 16:34
393 查看
转载:http://blog.csdn.net/loophome/article/details/51308631
我在一个业务中采用了按月的分表策略,当查询的条件跨月的时候,使用了union all汇总2个表的数据,并按插入时间倒序排列。查询并不复杂,但是当执行的时候却报错了。
[sql] view
plain copy
SELECT * FROM `table_201604` ORDER BY `REPORT_TIME` DESC
UNION ALL
SELECT * FROM `table_201605` ORDER BY `REPORT_TIME` DESC
[Err] 1221 - Incorrect usage of UNION and ORDER BY
报错的原因,是不正确使用UNION和ORDER BY指令,为什么呢?经过一番查找资料,才知道MySQL中UNION的语法是这样子的
[sql] view
plain copy
substatement union [all] substatement union [all] substatement [order by-clause] [limit-clause]
注意:
1. 每一个子句可以使用()包围,但是当第一个子句使用了()时,其它的子句都必须使用括号包围。
2. 每一个字句可以包含WHERE,GROUP BY,HAVING,JOIN,LIMIT,但是不能使用ORDER BY,如果需要使用ORDER BY,必须使用()包围子句。
接下来,做一些实验,能更好地理解,创建表和数据的SQL语句如下:
[sql] view
plain copy
CREATE TABLE `union_a` (
`ID` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增主键',
`T_NAME` varchar(255) CHARACTER SET utf8 NOT NULL DEFAULT ''COMMENT '所在表名称',
`NUMBER` int(11) NOT NULL DEFAULT '0' COMMENT '用于排序',
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `union_b` LIKE `union_a`;
INSERT INTO `union_a` VALUES ('1', 'a', '100');
INSERT INTO `union_a` VALUES ('2', 'a', '28');
INSERT INTO `union_a` VALUES ('3', 'a', '98');
INSERT INTO `union_a` VALUES ('4', 'a', '19');
INSERT INTO `union_a` VALUES ('5', 'a', '999');
INSERT INTO `union_b` VALUES ('1', 'b', '23');
INSERT INTO `union_b` VALUES ('2', 'b', '76');
INSERT INTO `union_b` VALUES ('3', 'b', '32');
INSERT INTO `union_b` VALUES ('4', 'b', '43');
INSERT INTO `union_b` VALUES ('5', 'b', '11');
一、关于LIMIT
执行以下SQL语句
[sql] view
plain copy
SELECT * FROM `union_a` LIMIT 1
UNION ALL
SELECT * FROM `union_b` LIMIT 1
可能第一眼看到这个SQL的时候,会想到将会返回2条记录,但是,结果却是只返回1条记录。回忆UNION的语法,最后一个LIMIT 1其实是对UNION之后的结果集取1条记录。
二、关于ORDER BY
我的需求是这样的,`union_a`和`union_b`表按NUMBER字段升序排列,最后将2个结果集合并,你可能想到的SQL会是这个样子
[sql] view
plain copy
SELECT * FROM `union_a` ORDER BY `NUMBER`
UNION ALL
SELECT * FROM `union_b` ORDER BY `NUMBER`
前面也说过,这个SQL语句是有语法错误的,但是,可以尝试这样,使用()将每个子句包围
[sql] view
plain copy
(SELECT * FROM `union_a` ORDER BY `NUMBER`)
UNION ALL
(SELECT * FROM `union_b` ORDER BY `NUMBER`)
SQL语句顺利执行了,但是,结果却不是我们想要的。看前5条记录,`union_a`表中的数据并没有按照NUMBER字段升序排列
查阅MYSQL的官方文档,其中包含对UNION中使用ORDER BY的说明:
对UNION中的子句应用ORDER BY是无效的,ORDER BY只能用于UNION后的整个结果集。如果需要对子句应用ORDER BY,必须添加LIMIT。正确的SQL如下
[sql] view
plain copy
(SELECT * FROM `union_a` ORDER BY `NUMBER` LIMIT 5)
UNION ALL
(SELECT * FROM `union_b` ORDER BY `NUMBER` LIMIT 5)
参考链接:
https://dev.mysql.com/doc/refman/5.7/en/union.html
http://stackoverflow.com/questions/6732661/incorrect-usage-of-union-and-order-by
这个是我的 Mapper.xml 的文件
<select id="findDesignListByOpenid" parameterType="String" resultType="java.util.HashMap" >
( select
dg.design_id as sid , dg.title as title , dg.showText as text, dg.picUrl as url , dg.endTime as endtime, 1 as joinin
from
form_data dt INNER JOIN form_design dg on dt.design_id = dg.design_id
where
dt.user_id=#{openId} AND dt.isDelete = '0'
ORDER BY dg.endTime
LIMIT 100 )
UNION ALL
( select
fg.design_id as sid , fg.title as title , fg.showText as text, fg.picUrl as url , fg.endTime as endtime, 0 as joinin
from
browse_form bf INNER JOIN form_design fg on bf.design_id = fg.design_id
where
bf.openid=#{openId}
ORDER BY fg.endTime
LIMIT 100 )
</select>
我在一个业务中采用了按月的分表策略,当查询的条件跨月的时候,使用了union all汇总2个表的数据,并按插入时间倒序排列。查询并不复杂,但是当执行的时候却报错了。
[sql] view
plain copy
SELECT * FROM `table_201604` ORDER BY `REPORT_TIME` DESC
UNION ALL
SELECT * FROM `table_201605` ORDER BY `REPORT_TIME` DESC
[Err] 1221 - Incorrect usage of UNION and ORDER BY
报错的原因,是不正确使用UNION和ORDER BY指令,为什么呢?经过一番查找资料,才知道MySQL中UNION的语法是这样子的
[sql] view
plain copy
substatement union [all] substatement union [all] substatement [order by-clause] [limit-clause]
注意:
1. 每一个子句可以使用()包围,但是当第一个子句使用了()时,其它的子句都必须使用括号包围。
2. 每一个字句可以包含WHERE,GROUP BY,HAVING,JOIN,LIMIT,但是不能使用ORDER BY,如果需要使用ORDER BY,必须使用()包围子句。
接下来,做一些实验,能更好地理解,创建表和数据的SQL语句如下:
[sql] view
plain copy
CREATE TABLE `union_a` (
`ID` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增主键',
`T_NAME` varchar(255) CHARACTER SET utf8 NOT NULL DEFAULT ''COMMENT '所在表名称',
`NUMBER` int(11) NOT NULL DEFAULT '0' COMMENT '用于排序',
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `union_b` LIKE `union_a`;
INSERT INTO `union_a` VALUES ('1', 'a', '100');
INSERT INTO `union_a` VALUES ('2', 'a', '28');
INSERT INTO `union_a` VALUES ('3', 'a', '98');
INSERT INTO `union_a` VALUES ('4', 'a', '19');
INSERT INTO `union_a` VALUES ('5', 'a', '999');
INSERT INTO `union_b` VALUES ('1', 'b', '23');
INSERT INTO `union_b` VALUES ('2', 'b', '76');
INSERT INTO `union_b` VALUES ('3', 'b', '32');
INSERT INTO `union_b` VALUES ('4', 'b', '43');
INSERT INTO `union_b` VALUES ('5', 'b', '11');
一、关于LIMIT
执行以下SQL语句
[sql] view
plain copy
SELECT * FROM `union_a` LIMIT 1
UNION ALL
SELECT * FROM `union_b` LIMIT 1
可能第一眼看到这个SQL的时候,会想到将会返回2条记录,但是,结果却是只返回1条记录。回忆UNION的语法,最后一个LIMIT 1其实是对UNION之后的结果集取1条记录。
二、关于ORDER BY
我的需求是这样的,`union_a`和`union_b`表按NUMBER字段升序排列,最后将2个结果集合并,你可能想到的SQL会是这个样子
[sql] view
plain copy
SELECT * FROM `union_a` ORDER BY `NUMBER`
UNION ALL
SELECT * FROM `union_b` ORDER BY `NUMBER`
前面也说过,这个SQL语句是有语法错误的,但是,可以尝试这样,使用()将每个子句包围
[sql] view
plain copy
(SELECT * FROM `union_a` ORDER BY `NUMBER`)
UNION ALL
(SELECT * FROM `union_b` ORDER BY `NUMBER`)
SQL语句顺利执行了,但是,结果却不是我们想要的。看前5条记录,`union_a`表中的数据并没有按照NUMBER字段升序排列
查阅MYSQL的官方文档,其中包含对UNION中使用ORDER BY的说明:
对UNION中的子句应用ORDER BY是无效的,ORDER BY只能用于UNION后的整个结果集。如果需要对子句应用ORDER BY,必须添加LIMIT。正确的SQL如下
[sql] view
plain copy
(SELECT * FROM `union_a` ORDER BY `NUMBER` LIMIT 5)
UNION ALL
(SELECT * FROM `union_b` ORDER BY `NUMBER` LIMIT 5)
参考链接:
https://dev.mysql.com/doc/refman/5.7/en/union.html
http://stackoverflow.com/questions/6732661/incorrect-usage-of-union-and-order-by
这个是我的 Mapper.xml 的文件
<select id="findDesignListByOpenid" parameterType="String" resultType="java.util.HashMap" >
( select
dg.design_id as sid , dg.title as title , dg.showText as text, dg.picUrl as url , dg.endTime as endtime, 1 as joinin
from
form_data dt INNER JOIN form_design dg on dt.design_id = dg.design_id
where
dt.user_id=#{openId} AND dt.isDelete = '0'
ORDER BY dg.endTime
LIMIT 100 )
UNION ALL
( select
fg.design_id as sid , fg.title as title , fg.showText as text, fg.picUrl as url , fg.endTime as endtime, 0 as joinin
from
browse_form bf INNER JOIN form_design fg on bf.design_id = fg.design_id
where
bf.openid=#{openId}
ORDER BY fg.endTime
LIMIT 100 )
</select>
相关文章推荐
- MYSQL之union和order by分析([Err] 1221 - Incorrect usage of UNION and ORDER BY)
- MySQL错误:ERROR 1221 (HY000): Incorrect usage of UNION and ORDER BY
- MySQL错误:ERROR 1221 (HY000): Incorrect usage of UNION and ORDER BY
- Incorrect usage of UNION and ORDER BY
- MySQL异常:[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggr
- mysql 5.7 [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonag
- MySQL错误:ERROR 1221 (HY000): Incorrect usage of DB GRANT and GLOBAL PRIVILEGES
- mysql主从复制报错 :Incorrect usage of DB GRANT and GLOBAL PRIVILEGES
- mysql 5.7会遇到 [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause ...的问题
- Get the average socre of the student.(The exmaple of group, having and order by).求学生平均分(group, having 和 order by使用示例).
- [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated c
- [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated c
- [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated c
- [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated.
- ERROR 1221 (HY000): Incorrect usage of DB GRANT and GLOBAL PRIVILEGES
- [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated c
- [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated..
- [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated c
- [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated c
- [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated c