关于SQL三张表的按条件查询查询

yichen520sen 2010-04-20 03:35:19
三张表company,Phone,Visit

company表字段
Id Name,adrress,Phone,Moblie
Phone表字段
Id ,CompanyId(外键), appdate,appContent,
Visit表字段
Id,CompanyId(外键),visitDate,visitContent.


我要在页面上按条件查询;company的Name,Phone的appdate,,Visit的visitDate 三个条件查询
怎么写这个SQL语句 GridView显示 Name,adrress,Phone,Moblie,appdate,visitDate的值

请高手指点
...全文
218 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
yichen520sen 2010-04-20
  • 打赏
  • 举报
回复
删除一个客户Id其他两张表的内容全都删除。。。请高手写个高效率的SQL语句,,,要是数据量50000以上,普通的SQL语句会瘫痪的。。
yichen520sen 2010-04-20
  • 打赏
  • 举报
回复
那三张表删除呢。。。
daishaodong 2010-04-20
  • 打赏
  • 举报
回复
只查询出匹配的记录,用inner join 加on,或2楼的方法,

若左表的表要列出全部记录,用left inner 加 on条件
dawugui 2010-04-20
  • 打赏
  • 举报
回复
--如果你能确保各表所有数据都能关联.
select t1.name , t1.adrress,t1.phone,t1.mobile,t2.appdate,t3.visitDate
from company t1 , Phone t2,Visit t3
where t1.Id = t2.CompanyId and t1.id = t3.CompanyId


--否则需要使用下面的语句
select t1.name , t1.adrress,t1.phone,t1.mobile,t2.appdate,t3.visitDate from company t1 
left join Phone t2 on t1.Id = t2.CompanyId
left join Visit t3 on t1.id = t3.CompanyId

select t1.name , t1.adrress,t1.phone,t1.mobile,
appdate = (select appdate from Phone t2 where t2.CompanyId = t1.Id),
visitDate = (select visitDate from Visit t3 where t3.CompanyId = t1.Id)
from company t1
dawugui 2010-04-20
  • 打赏
  • 举报
回复
select t1.name , t1.adrress,t1.phone,t1.mobile,t2.appdate,t3.visitDate
from company t1 , Phone t2,Visit t3
where t1.Id = t2.CompanyId and t1.id = t3.CompanyId
--小F-- 2010-04-20
  • 打赏
  • 举报
回复
select
a.name,adrress,phone,moblie,b.appdate,c.visitdate
from
company a ,Phone b mvisit c
where
a.id=b.companyid
and
a.id=c.companyid
chuifengde 2010-04-20
  • 打赏
  • 举报
回复
select a.name,adrress,phone,moblie,b.appdate,c.visitdate
from company a inner join Phone b on a.id=b.companyid
inner join visit c on a.id=c.companyid

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧