怎么写动态sql哪里条件? [英] how to write dynamic sql where condition ?
本文介绍了怎么写动态sql哪里条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
i有一个非常奇怪的情况,我需要在sql查询的where子句中写一个动态条件。
我的问题是什么?
i有三张桌子...
1)表A - > EmpID int
2)表B - > EmpID int
2)表C - > EmpID int
现在我想要的是检查
这样的东西..
HI all ,
i have a very weird situation where i need to write a dynamic condition in the where clause in sql query.
what is my issue is
i have three tables...
1) table A - > EmpID int
2) table B - > EmpID int
2) table C - > EmpID int
NOW what i want is to check
something like this ..
SELECT * ....
FroM A,b,c
WHERE a.empid = b.empid and a.empid = c.empid ....
现在如果这个条件不成立我需要申请
now if this condition is not true i need to apply
a.empid = b.empid OR a.empid = c.empid
两者同时不会工作。
both simultenously will not work.
推荐答案
请阅读我对这个问题的评论。
我建议从这里开始: SQL联接的可视化表示 [ ^ ]了解join
s而不是where
子句。
如果你想比较empid的,哪些是哪个是不在表2和表3中,使用 IN [ ^ ]或 EXISTS [ ^ ]条款。
Please, read my comment to the question.
I would suggest to start here: Visual Representation of SQL Joins[^] to learn aboutjoin
s insteadwhere
clause.
If you want to compare empid's, which are and which are not in table2 and table3, use IN[^] or EXISTS[^] clause.
SELECT empid
FROM Table1
WHERE NOT IN (SELECT empid FROM Table2)
尝试加入查询
try a join query
SELECT * From A as a
inner join b as on a.empid = b.empid
inner join c as c on a.empid = c.empid
这篇关于怎么写动态sql哪里条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文