select * from wat_cominfo a
left join Wat_ProComRelation b on b.Com_ID = a.Com_ID
where exists (select 1 from Wat_ProductInfo where Pro_Name = '苹果' and Pro_ID= b.Pro_ID)结果157条记录.
select * from wat_cominfo where Com_ID in(
select Com_ID from Wat_ProComRelation
where Pro_ID in (select top 1 Pro_ID from Wat_ProductInfo where Pro_Name = '苹果')
)结果88条记录.Wat_ProComRelation 是wat_cominfo(企业)和Wat_ProductInfo(产品)的关联表, 我要查询有"苹果"产品的企业.请问哪一个是正确的?? 有什么不同?? 其实我已经知道了, 只是想听听别人是怎么想的, 我以前一遇到关联表就用left join , 没有想过这样的问题. 努力学习!!!
解决方案 »
- Repeater嵌套checkbox如何在后台取的值
- 谈一谈asp.net网站的性能优化?
- vs2010 aspx页面假死
- Silverlight + WCF + LINQ ,三层怎么分呀(silverlight、linq板块人气不旺,WCF暂无板块,只有发这里了)?
- WEB的数据库类 把 connection 对象设置成 static 有什么好处?
- ASP.NET中调用存储过程后,运行无错误,但数据库没有任何改变
- 奇怪问题
- 怎样确定当前记录是数据表中的第几条!
- 用asp或asp.net开发网站,哪种更好些,各有什么优缺点?
- datagrid中怎样用复选框实现多项删除?
- ObjectDataSource 未能找到带参数的非泛型方法
- 关于网站视频播放的问题
这样可能更直观一点,不知道楼主是不是这个意思。
select * from wat_cominfo where Com_ID in(
select Com_ID from Wat_ProComRelation
where Pro_ID = (select top 1 Pro_ID from Wat_ProductInfo where Pro_Name = '苹果')
)同意楼上的,或者这样也可以。
这样更容易扫描索引页,不会进行表扫描。
select Com_ID from Wat_ProComRelation
where Pro_ID in (select top 1 Pro_ID from Wat_ProductInfo where Pro_Name = '苹果')--這邊top幹嘛?
)
第二种方法是内连接,需要com_id同时在两个表中存在,而且在Wat_ProComRelation中要满足给出的条件
这是sql基本语法。
还有右连接,交叉连接。