CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  基础类

这个关联两张表的sql语句该怎么写?

楼主jhsfg(小虾ww)2005-02-04 11:39:50 在 MS-SQL Server / 基础类 提问

有两张表  
  table   1  
  id     name    
    1       text1    
    2       text2  
    3       text3  
    4       text4  
  table2  
   
    id   tb1id   content  
      1     2         content1  
      2     3         content2  
   
  其中table1的id   与table2的tblid相对应(但是table1中的ID在table2中不一定有的)  
    我要取出以下数据:  
  -----------------------  
    1       text1      
    2       text2     content1  
    3       text4     content2  
    4       text5  
   
  用select   a.id,a.name,b.content   from   table1   a,table2   b   where   a.id=b.tb1id  
  只能取出两条数据  
   
  问题点数:60、回复次数:8Top

1 楼libin_ftsafe(子陌红尘:TS for Banking Card)回复于 2005-02-04 11:47:16 得分 0

select    
          a.id,  
          a.name,  
          b.content    
  from    
          table1   a    
  left   join  
          table2   b    
  on  
          a.id=b.tb1idTop

2 楼skyboy0720(曲终人散)回复于 2005-02-04 11:47:24 得分 10

select   a.id,a.name,b.content   from   table1   a   left   join   table2   b   on   a.id=b.tb1id  
  Top

3 楼llhly(无名)回复于 2005-02-04 11:54:55 得分 0

select   a.id,a.name,b.content,b.tb1id   from   table1   a,table2   b   where   a.id=b.tb1id   and   a.id   in   (select   tb1id   from   table2)Top

4 楼libin_ftsafe(子陌红尘:TS for Banking Card)回复于 2005-02-04 11:56:34 得分 40

select   a.id,a.name,b.content   from   table1   a,table2   b   where   a.id*=b.tb1idTop

5 楼xluzhong(Ralph)回复于 2005-02-04 11:59:53 得分 10

select   a.id,a.name,b.content    
  from   table1   a  
  left   join  
  table2   b    
  where   a.id=b.tb1id  
  Top

6 楼xluzhong(Ralph)回复于 2005-02-04 12:00:03 得分 0

select   a.id,a.name,b.content    
  from   table1   a  
  left   join  
  table2   b    
  where   a.id=b.tb1idTop

7 楼xluzhong(Ralph)回复于 2005-02-04 12:00:59 得分 0

或者  
  select   a.id,a.name,b.content   from   table1   a,table2   b   where   a.id=b.tb1id  
  union    
  select   a.id,a.name,b.content   from   table1   a,table2   b   where   a.id<>b.tb1idTop

8 楼tgwandqn(yaterman)回复于 2005-02-04 12:19:35 得分 0

up  
  Top

相关问题

  • 表关联的sql语句(急!)
  • 表关联sql语句问题,谢谢
  • 关于表关联时SQL语句的关联条件
  • 求一关联sql语句
  • 关联表语句问题。
  • 两个关联表的更新的SQL语句问题?
  • 求一条表关联的sql语句!急啊!
  • 三表关联,这个sql语句应该怎么写?
  • 100分求SQL语句:多表关联查询,高手请进~
  • 请教一个两表关联判断的SQL语句

关键词

  • idselect
  • tb
  • content
  • table
  • where
  • text
  • select

得分解答快速导航

  • 帖主:jhsfg
  • skyboy0720
  • libin_ftsafe
  • xluzhong

相关链接

  • SQL Server类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo