急,急,如何将sql查询的多个结果集进行 与和或 操作,在线等待,高手帮忙 (200分)

T

tjj

Unregistered / Unconfirmed
GUEST, unregistred user!
多个查询语句如
selece a1 from tba where (条件1) :表达式1
select a1 from tbb where (条件2) :表达式2
select a1 from tbc where (条件3) :表达式3
tba,tbb,tbc是三个表都有a1这个字段,如何将这三个查询结果进行
与和或操作返回一个结果集。例如 1+2*3 的结果
请高手指教,在下感恩不尽
在线等待。
 
selece a1 from tba where (条件1)
union
select a1 from tbb where (条件2)
union
select a1 from tbc where (条件3)
 
to QuickSilver:
我要得结果不仅是合并,
是表达式 1*2+3 类似的条件结果还有括号 (1+2*3)+之类的
 
说具体点
 
具体的:
我的库中有多个表,每个表都有a1,字段,我要根据多个表的条件查询
返回符合条件的a1的集合,
例如:表1 中有性别字段
表2 中有学历字段
表3 中有职称字段
我要根据这三个表的条件查询结果,但条件间的关系可能是与关系,也可能是或关系
比如:我要查 性别=男 或 学历=大本 的这些人中职称是工程师的人
也就是 (性别=男查 + 学历=大本)*职称=工程师
明白了吗?
 
selece (tba.a1+tbb.a1*tbc.a1) as aa from tba,tbb,tbc where (条件1) and (条件2) and (条件3)
 
同意楼上的
 
select AA.a1
from (select a1 from 表1 where 性别=男
union
select a1 from 表2 where 学历=大本) AA,
(select a1 from 表3 where 职称=工程师) BB
where AA.a1=BB.a1
 
select d1.a1 from
(selece a1 from tba where (条件1)
union
select a1 from tbb where (条件2)) d1,
select a1 from tbc where (条件3) d2
where d1.a1=d2.a1
以上是(1+2)*3
另外类推
 
我的表达式是从字符串中动态分析出来的,
这个表达式的sql语句怎么动态生成最后的where 子句?
请大家继续帮忙!
 
用join不就行了,join兩次
 
select a1 from tba where (条件1) and
(a1 in (select a1 from tbb where (条件2) and
(a1 in (select a1 from tbc where (条件3)))
)

不好意思,括号多了点
 
adoquery1.close;
adoquery1.sql.clear;
adoquery1.sql.add('select AA.a1');
adoquery1.sql.add('from (select a1 from 表1 where 性别=:para1 union select a1 from 表2 where 学历=:para2) AA,');
adoquery1.sql.add('(select a1 from 表3 where 职称=:para3) BB ');
adoquery1.sql.add('where AA.a1=BB.a1');
adoquery1.Parameters.ParamByName('para1').value:='男';
adoquery1.Parameters.ParamByName('para2').value:='大本';
adoquery1.Parameters.ParamByName('para3').value:='工程师';
adoquery1.open;

用以上的方式把参数传到SQL语句中,以上代码未验证,大致是如此。
 
我的表达式可能有十多个,而且是动态生成sql语句。
大家的方法没问题,就是让程序自动生成可能会有问题。
 
做法类似于公式解析
由于没有优先级
还要容易一些
 
selece A.a1+B.a1*C.a1 from tba A,tbb B,tbc C where A.条件1:表达式1
and B.条件2:表达式2
and C条件3:表达式3
 
谢谢大家帮忙,现在给你们分分,我最终的解决方案是
做了一个分析程序,先把各个表达式的结果查出来,再把他们作为
操作数,按照操作符优先级进行运算。因为如果连接太多了,sql server
太慢,大概等于死机。这样做复杂一点,但效率会好,而且表达式个数没限制

 
多人接受答案了。
 
顶部