您好,欢迎访问一九零五行业门户网

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是虚拟列,值是列的名字
其它类似信息

推荐信息