Sql语句帮助 - 不确定是否可以在一个sql语句中执行它 [英] Sql statement help - not sure whether it is possible to do it in one sql statement

查看:114
本文介绍了Sql语句帮助 - 不确定是否可以在一个sql语句中执行它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在使用SQL Server 2008.我有3个表。表a有1列辅助,表b有1列出价,表c有2列辅助,出价。是否可以使用一个SQL语句返回表c中存在 NOT 的所有援助和出价组合。



例如表a有2行:a,b

表b有3行1,2,3

表c有2行(a,2),(b,1 )



我希望将记录集返回4行:(a,1),(a,3),(b,2),(b, 3)



提前感谢您的帮助。



Cathy

Hi,

I am using SQL server 2008. I have 3 tables. Table a has 1 column aid, table b has 1 column bid and table c has 2 columns aid, bid. Is it possible to use one SQL statement that returns all the combination of aid and bid that does NOT exists in table c.

e.g. table a has 2 rows: a, b
table b has 3 rows 1,2,3
table c has 2 rows (a, 2), (b,1)

and I want to return record set to be 4 rows: (a,1), (a,3), (b,2), (b,3)

Thanks in advance for any help.

Cathy

推荐答案

您可以生成表a和b笛卡尔积,然后从中减去c集以查看结果。对于减法,使用运算符。更多研究TSQL的读取集函数:

http://technet.microsoft .com / zh-CN / library / ms188055.aspx [ ^ ]



所以你的SQL是这样的:

You can produce table a and b cartesian product and then subtract the c set from it to see the result. For subtraction use except operator. for more study read set functions of TSQL :
http://technet.microsoft.com/en-us/library/ms188055.aspx[^]

So your SQL is this :
select aid, bid from a,b as cartesian
except
select aid, bid from c





好​​运



Good Luck


当然,我想到的是使用公用表表达式形成一组所有关系,然后使用表C进行外连接,并在CTE中形成一组不在C中的元素。
Sure, what comes to mind is using a Common Table Expression to form a set of all the relationships and then Outer Join that with table C and form a set of the elements in the CTE that are not in C.


这篇关于Sql语句帮助 - 不确定是否可以在一个sql语句中执行它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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