首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

帮写一条两个表连接的话语

2013-01-06 
帮写一条两个表连接的语句表usermessage(id,username,password,touxiang)和表table20(id,username,title,t

帮写一条两个表连接的语句

表usermessage(id,username,password,touxiang)和表table20(id,username,title,time,content),
其中usermessage和table20中有一共同字段username,
现在我想得到这样的结果:显示 表A的username,touxiang,表B的title、time、content ,
where条件是根据table20的id来查询,传入的参数是string id4
大概代码如下:
   string SqlStr4="select username,touxiang,title,time,content from usermessage and table20 where  table20.id='"+id4+"'";

[解决办法]

select a.username,a.touxiang,b.title,b.time,b.content from usermessage a
left join table20 b on a.username=b.username
where b.id=

[解决办法]
select a.username,a.touxiang,b.title,b.time,b.content from usermessage a
left join table20 b on a.username=b.username
where b.id=

[解决办法]

string SqlStr4="select a.username,a.touxiang,b.title,b.time,b.content from usermessage a,table20 b where a.username=b.username and b.id='"+id4+"'";

[解决办法]



SELECT  a.username ,
        a.touxiang ,
        b.title ,
        b.time ,
        b.content
FROM    usermessage a
        INNER JOIN table20 b ON A.username = b.username
WHERE   b.id = @id

[解决办法]
select username,touxiang,title,time,content 
from usermessage a left join table20 b 
on a.id=b.id and a.username=b.username
where  b.id='"+id4+"'"

热点排行