union组合结果集时的order问题
如果能确定各查询结果不会有重复的项,最好就带上all,因为这样还是可以提高一些效率的。
近日,在一个项目中用到union组合两个select结果,调试sql时总是报错,所报错误也只是说在union附近有问题,因为sql中用到了group,我想也许是union不支持吧,由于时间紧,就先在程序中做了合并处理。但程序员对于代码的完美性要求总是不能放弃的,所以,常常会有如鲠在喉的感觉,不搞明白心里会不舒服。仔细查看了MS的在线帮助,在有关UNION的描述中找到这样一段说明:
如果使用 UNION 运算符,那么各个 SELECT 语句不能包含它们自己的 ORDER BY 或 COMPUTE 子句。而只能在最后一个 SELECT 语句的后面使用一个 ORDER BY 或 COMPUTE 子句;该子句适用于最终的组合结果集。只能在各个 SELECT 语句中指定 GROUP BY 和 HAVING 子句。
在这段说明中并没有说union不能用于group,而是说在各个Select中不能包含order by,而在我的语句中恰恰就有这个,看来是我猜错了。试了一下把order去掉,果然不会报错了。也就是说,使用union的时候,各查询group是可以的,但不能order或compute。那如果说非要group,有没有办法呢?正常情况下只能在最后使用,而且是针对组合后的结果集进行排序的,而我刚才所说的group,就不能用于最终结果集,而只能用于每个查询。
这是正常的用法,有些人想在每个查询中先排序,然后再union,也有非正常的用法,类似:
select * from (select a from [table] order by a) union ...
另外,union后面还可以加上all,在默认情况下,union时会删除重复的项,如果加上all则不进行筛选,组合所有的结果。如果能确定各查询结果不会有重复的项,最好就带上all,因为这样还是可以提高一些效率的。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The use of union in C language is a special data type that allows different data types to be stored in the same memory location. The use of union can help us save memory space and facilitate conversion between different data types. When using union, you need to note that the corresponding member is valid and only one member can be accessed at the same time.

How to use the FULLOUTERJOIN function in MySQL to obtain the union of two tables. In MySQL, the FULLOUTERJOIN function is a powerful join operation that combines inner joins and outer joins. It can be used to get the union of two tables, that is, combine all the data in the two tables into a single result set. This article will introduce the usage of the FULLOUTERJOIN function and provide some sample code to help readers better understand. FULLOUTERJOIN function

1. Union is not a method of multi-table connection query. It combines the query results of multiple query sentences into one result and removes duplicate data. 2. Full outer join queries the data of the left table and the right table, and then connects according to the connection conditions. Example #Use the left outer Aunion and the right outer BSELECT*FROMt_categorycLEFTOUTERJOINt_productpONc.cid=p.cnounionSELECT*FROMt_categorycRIGHTOUTERJOINt_productpONc.cid=p.cno

Define the Union class to implement the coexistence of data bodies. In the C/C++ language, a union, also known as a union, is a data structure similar to a struct. A union, like a struct, can contain many data types and variables. The difference between the two is as follows: all variables in a struct "coexist", and all variables are effective at the same time. Each variable occupies Different memory spaces; in a union, each variable is "mutually exclusive", only one variable is effective at the same time, and all variables occupy the same memory space. When multiple data need to share memory or only one of multiple data needs to be taken at a time, a union can be used. in Java

Optimize the Like statement with Union 1) Sometimes, you may need to use the or operator for comparison in the query. When the or keyword is used too frequently in the where clause, it may cause the MySQL optimizer to mistakenly choose a full table scan to retrieve records. The union clause can make queries execute faster, especially when one of the queries has an optimized index and the other query also has an optimized index. For example, when there are indexes on first_name and last_name respectively, execute the following query statement: mysql>select*fromstudentswherefirst_namelike'A

In many database applications, we are faced with situations where we need to integrate data from multiple data sources. MySQL's UNION statement is a way to solve this situation, which allows us to merge the result sets of two or more SELECT statements into one. While this is a very convenient feature, UNION statements can also cause performance issues on your system if not optimized. This article will explore how to optimize UNION to improve performance through MySQL. Use UNIONALL while using U

Fetching a row from a result set as an associative array using the PHP function "mysqli_fetch_assoc" In PHP, interacting with a database is a common task. When we execute a SELECT query and get the result set, we usually need to store the data in the result set into a PHP array for further processing. PHP provides multiple functions to process result sets, one of the commonly used functions is "mysqli_fetch_assoc". This function gets a row from the result set

1. The union operator is used to combine the results of two or more select statements into a result set. Multiple select statements will delete duplicate data. 2. When using union to merge result sets, the number of columns in the two result sets is required to be the same. Exampleselectplayerno,townfromPLAYERSwheretown='Inglewood' unionselectplayerno,townfromPLAYERSwheretown='Plymouth';
