从多表中选择 [英] Select from multi tables

查看:116
本文介绍了从多表中选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从sql语句中的多个表(约50个表)中选择1个字段?

我需要获取一个表包含,Date字段然后是50个字段,每个字段来自一个表。

使用了这个语句,但它不能超过31个表,当放置超过31个表时出现错误信息(查询过于复杂)

 选择 t1.day_date,t1.at_max,t2.at_max,t3.at_max,t4.at_max,t5.at_max,t6.at_max,t7.at_max, t8.at_max,t9.at_max,t10.at_max,t11.at_max,t12.at_max,t13.at_max,t14.at_max,t15.at_max,t16.at_max,t17.at_max,t18.at_max,t19.at_max,T20。 at_max,t21.at_max,t22.at_max,t23.at_max,t24.at_max,t25.at_max,t26.at_max,t27.at_max,t28.at_max,t29.at_max,t30.at_max,t31.at_max 
< span class =code-keyword>来自 t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16,t17,t18, t19,t20,t21,t22,t23,t24,t25,t26,t27,t28,t29,t30,t31
其中
(t1 .day_date = t2.day_date)(t1.day_date = t3.day_date)(t1.day_date = t4.day_date)(t1.day_date = t5.day_date)(t1.day_date = t6.day_date)(t1.day_date = t7.day_date)(t1.day_date = t8.day_date)(t1.day_date = t9.day_date)(t1.day_date = t10.day_date)(t1.day_date = t11.day_date)(t1.day_date = t12.day_date) (t1.day_date = t13.day_date)(t1.day_date = t14.day_date)(t1 .day_date = t15.day_date)(t1.day_date = t16.day_date)(t1.day_date = t17.day_date)(t1.day_date = t18.day_date)(t1.day_date = t19.day_date)(t1.day_date = t20.day_date)(t1.day_date = t21.day_date)(t1.day_date = t22.day_date)(t1.day_date = t23.day_date)(t1.day_date = t24.day_date)(t1.day_date = t25.day_date)(t1.day_date = t26.day_date)(t1.day_date = t27.day_date) (t1.day_date = t28.day_date)(t1.day_date = t29.day_date)(t1 .day_date = t30.day_date)(t1.day_date = t31.day_date)(t1.day_date #2014/01/01##2014/01/31#)

解决方案

在多个查询中划分您的查询并将结果添加到临时表

1.用所有列创建1个临时表

2.Fire第一次查询15个表并在临时表中添加结果

3.Fire第二个查询15个表并在临时表中添加结果

4.临时表的响应结果


你可以用 UNION 来做。

见,

http://stackoverflow.com/questions/17092697/select-same -column-from-multiple-tables-only-something-something-something [ ^ ]



-KR


<集团kquote>

  SELECT    FROM  table1 
UNION
SELECT FROM table2
UNION
SELECT FROM table3 ...


How do I select 1 field from multiple tables (about 50 tables) in sql statement?
I need to get a table contains, Date field then the 50's fields each field from a table.
Used this statement but it doesn't work form more than 31 tables, when put more than 31 tables error message says (Query is too complex)

select t1.day_date,t1.at_max,t2.at_max,t3.at_max,t4.at_max,t5.at_max,t6.at_max,t7.at_max,t8.at_max,t9.at_max,t10.at_max,t11.at_max,t12.at_max,t13.at_max,t14.at_max,t15.at_max,t16.at_max,t17.at_max,t18.at_max,t19.at_max,t20.at_max,t21.at_max,t22.at_max,t23.at_max,t24.at_max,t25.at_max,t26.at_max,t27.at_max,t28.at_max,t29.at_max,t30.at_max,t31.at_max
from t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16,t17,t18,t19,t20,t21,t22,t23,t24,t25,t26,t27,t28,t29,t30,t31
 where
(t1.day_date = t2.day_date) and (t1.day_date = t3.day_date) and (t1.day_date = t4.day_date) and (t1.day_date = t5.day_date) and (t1.day_date = t6.day_date) and (t1.day_date = t7.day_date) and (t1.day_date = t8.day_date) and (t1.day_date = t9.day_date) and (t1.day_date = t10.day_date) and (t1.day_date = t11.day_date) and (t1.day_date = t12.day_date) and (t1.day_date = t13.day_date) and (t1.day_date = t14.day_date) and (t1.day_date = t15.day_date) and (t1.day_date = t16.day_date) and (t1.day_date = t17.day_date) and (t1.day_date = t18.day_date) and (t1.day_date = t19.day_date) and (t1.day_date = t20.day_date) and (t1.day_date = t21.day_date) and (t1.day_date = t22.day_date) and (t1.day_date = t23.day_date) and (t1.day_date = t24.day_date) and (t1.day_date = t25.day_date) and (t1.day_date = t26.day_date) and (t1.day_date = t27.day_date) and (t1.day_date = t28.day_date) and (t1.day_date = t29.day_date) and (t1.day_date = t30.day_date) and (t1.day_date = t31.day_date) and  (t1.day_date between #2014/01/01# and #2014/01/31#)

解决方案

Divide your query in multiple query and add result to temp table
1.Create 1 temp table with all column
2.Fire first query for 15 table and add result in temp table
3.Fire second query for 15 table and add result in temp table
4.retrive result for temp table


You can do that using UNION.
See,
http://stackoverflow.com/questions/17092697/select-same-column-from-multiple-tables-only-where-something-something[^]

-KR


SELECT Column FROM table1
UNION
SELECT Column FROM table2
UNION
SELECT Column FROM table3 ...


这篇关于从多表中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆