mysql 多表合并查询
http://hi.baidu.com/magicweng/item/094ffd1808ee945bf0090e44 http://blog.jhonse.com/index.php/archives/2076.jhonse 挺有用的就记录下来了 一UNION 语法 SELECT ... UNION[ALL | DISTINCT] SELECT ... [UNION [ALL | DISTINCT] SELECT ...] UNION 用于
http://hi.baidu.com/magicweng/item/094ffd1808ee945bf0090e44
http://blog.jhonse.com/index.php/archives/2076.jhonse挺有用的就记录下来了
一UNION语法
SELECT ...
UNION[ALL | DISTINCT]
SELECT ...
[UNION [ALL | DISTINCT]
SELECT ...]
UNION用于把来自许多SELECT语句的结果组合到一个结果集合中。
对于每个SELECT语句的对应位置的被选择的列应具有相同的类型。(例如,被第一个语句选择的第一列应和被其它语句选择的第一列具有相同的类型。)(注:我在mysql5.5版本测试的,如果两个表对应的列属性的类型不相同,也能执行语句,大家也可验证下)在第一个SELECT语句中被使用的列名称也被用于结果的列名称(需要注意的是各个查询语句select的列的个数要保持相同,比如你可以用select * from ... UNINO ALL select * from ... UNION ALL... ,当然了这时你要保证select的列的个数要保持一致,不用select *时,就不用啰嗦了,哈哈哈)。
SELECT语句为常规的选择语句,但是受到如下的限定:
·只有最后一个SELECT语句可以使用INTO OUTFILE。
·HIGH_PRIORITY不能与作为UNION一部分的SELECT语句同时使用。如果您对第一个 SELECT指定了HIGH_PRIORITY,则不会起作用。如果您对其它后续的SELECT语句指定了HIGH_PRIORITY,则会产生语法错误。
如果您对UNION不使用关键词ALL,则所有返回的行都是唯一的,如同您已经对整个结果集合使用了DISTINCT。如果您指定了ALL,您会从所有用过的SELECT语句中得到所有匹配的行。 比如SELECT VALUE fromcode UNION ALL (SELECT NAME from neeq_code) 不去重 ;去掉All 则去重
DISTINCT关键词是一个自选词,不起任何作用,但是根据SQL标准的要求,在语法中允许采用。(在MySQL中,DISTINCT代表一个共用体的默认工作性质。)
您可以在同一查询中混合UNION ALL和UNION DISTINCT。被混合的UNION类型按照这样的方式对待,即DISTINCT共用体覆盖位于其左边的所有ALL共用体。DISTINCT共用体可以使用UNION DISTINCT明确地生成,或使用UNION(后面不加DISTINCT或ALL关键词)隐含地生成。
如果您想使用ORDER BY或LIMIT子句来对全部UNION结果进行分类或限制,则应对单个地SELECT语句加圆括号,并把ORDER BY或LIMIT放到最后一个的后面。以下例子同时使用了这两个子句:
(SELECT a FROM tbl_name WHERE a=10 ANDB=1)UNION(SELECT a FROM tbl_name WHERE a=11 AND B=2)ORDER BY a LIMIT 10;
(SELECT a FROM tbl_name WHERE a=10 AND B=1)UNION(SELECTa FROM tbl_name WHERE a=11 AND B=2)ORDER BY a LIMIT 10;
这种ORDER BY不能使用包括表名称列引用(也就是,采用tbl_name.col_name格式的名称)。可以在第一个SELECT语句中提供一个列别名(第二个select语句提供列别名就不行),并在ORDER BY中参阅别名,或使用列位置在ORDER BY中参阅列。(首选采用别名,因为不建议使用列位置。)
另外,如果带分类的一列有别名,则ORDER BY子句必须引用别名,而不能引用列名称。以下语句中的第一个语句必须运行,但是第二个会运行失败,出现在'order clause'中有未知列'a'的错误:
(SELECT a AS b FROM t) UNION (SELECT...) ORDER BY b;
(SELECT a AS b FROM t) UNION (SELECT...) ORDER BY a;
To apply ORDER BY or LIMIT to anindividual SELECT, place the clause inside the parentheses that enclose theSELECT。
为了对单个SELECT使用ORDERBY或LIMIT,应把子句放入圆括号中。圆括号包含了SELECT。
(SELECT a FROM tbl_name WHERE a=10 ANDB=1 ORDER BY a LIMIT 10)UNION(SELECT a FROM tbl_name WHERE a=11 AND B=2 ORDERBY a LIMIT 10);
这样共取二十条数据
(SELECT VALUE from code order by VALUEdesc LIMIT 4) UNION ALL (SELECT VALUE from neeq_code order by VALUE desc LIMIT4) ORDER BY VALUE desc LIMIT 4 ; 取得4条数据,最后的ORDERBY VALUE desc LIMIT 4 是对整个结果在排序,并且取四条
二 实例扩展
union可以对同一个表的两次查询联合起来.这样做的益处也非常明显, 比如在blog应用中, 可以利用一条sql语句实现置顶blog和普通blog的分页显示.
(
SELECT * FROM `blog`
WHERE top=1
ORDER BY created DESC
)
UNION
(
SELECT *
FROM `blog`
WHERE top = 0
ORDER BY created DESC
) LIMIT 2 , 3
注:union要求联合的两个表所要查找的数据列要一样多(这里针对的是select *这种情况吧),如果一个表中没有另一个表的字段,可以用NULL代替。
//union后表中数据的总条数
例:SELECT count(*) from (SELECT * from code UNION ALL SELECT* from neeq_code) as t; 如果去掉t就报错...
//union后表的按条件查询SELECT t.* from (SELECT * from code UNION ALL SELECT* from neeq_code) as t where t.字段名=‘’ ;
//虚拟列
SELECT value,name,'2013'from code UNION ALL SELECT value,name,'2014' from neeq_code;
2013,2014是虚拟列,值是列的名字

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.
