如何效率的查询多个表中的同一字段的数据

来源:百度知道 编辑:UC知道 时间:2024/05/14 10:37:08
例如有A,B,C,D,E5张表 5张表的字段先同都为a,b如何有效率的查询5张表返回一条数据。
我几乎是每天生成一个表,这样的话1年就会有365张表 表的命名规则打个比方就是2009年1月1日的表定义为table20090101 那么2009年1月2日的表名就是table20090202 那么在程序中我想查询2009年1月份的数据,该如何查询呢?如果查询1年的话怎么办?目前想到的解决方法中都感觉效率不是很高,有高人帮忙解决问题吗?

如果你的表有关系字段的话,比如每个表的a字段为关联字段:

select a.*,b.b,c.b,d.b,e.b from
a inner join b on a.a=b.a
inner join c on a.a=c.a
inner join d on a.a=d.a
inner join e on a.a=e.a;

---
以上,希望对你有所帮助。

那要看你返回什么样的数据?
你的意思是?
select top 1 a.a from
(select a,b from table1 union all
select a,b from table2 union all
select a,b from table3 union all
select a,b from table4 union all
select a,b from table5) a