大家帮忙看个T-SQL语句问题?

rwh871212 2011-09-07 04:50:01
有三张表,我先是根据通过类型表newsclass(name)的ID 好和news(type)关联的得到news 表的ID,再通过imgList 的(type)和newsclass(name)关联,想通过另一个程序上传一张图片得到news表的ID,可如果按news(updatedate)排序,查出来的news表的ID一样,按 imgList (updatedate)排序,查出来的imgList 表的图片字段(img)一样,请问怎么解决,高手指导?
code=C#]
select * from newsclass where name ='企业风采'
select * from news where type ='6efabe27-c274-4106-8560-9f012536e9bb'
select * from imgList where type ='企业风采 '
[/code]



我写的代码如下:

select top 4
X.Id,X.title,X.context,X.name,X.type,X.img,X.updatedate ,X.date from
(
select distinct NW.Id,NW.title,NW.context,NW.updatedate as updatedate,NC.name,IG.type,IG.img ,IG.updatedate as date
from news as NW
left join newsclass as NC on NW.type =NC.id
left join ImgList as IG on NC.Name =IG.type
)as X
where X.type ='企业风采'
order by X.date desc


出现问题的效果如下:
id相同:


图片相同
...全文
73 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
rwh871212 2011-09-07
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 liuchaolin 的回复:]
select top 4
X.Id,X.title,X.context,X.name,X.type,X.img,X.updatedate ,X.date from
(
select distinct NW.Id,NW.title,NW.context,NW.updatedate as updatedate,NC.name,IG.type,IG.img ,IG.updatedate……
[/Quote]

不好意思,看错了,好想可以我测一下
rwh871212 2011-09-07
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 liuchaolin 的回复:]
select top 4
X.Id,X.title,X.context,X.name,X.type,X.img,X.updatedate ,X.date from
(
select distinct NW.Id,NW.title,NW.context,NW.updatedate as updatedate,NC.name,IG.type,IG.img ,IG.updatedate……
[/Quote]

select top 4
X.Id,X.title,X.context,X.name,X.type,X.img,X.updatedate ,X.date from
(
select distinct NW.Id,NW.title,NW.context,NW.updatedate as updatedate,NC.name,IG.type,IG.img ,IG.updatedate as date
from news as NW
right join newsclass as NC on NW.type =NC.id
right outer join ImgList as IG on NC.Name =IG.type
)as X
where X.type ='企业风采'
group by X.Id,X.title,X.context,X.name,X.type,X.img,X.updatedate ,X.date
order by X.updatedate,X.date desc
md5e 2011-09-07
  • 打赏
  • 举报
回复
select top 4
X.Id,X.title,X.context,X.name,X.type,X.img,X.updatedate ,X.date from
(
select distinct NW.Id,NW.title,NW.context,NW.updatedate as updatedate,NC.name,IG.type,IG.img ,IG.updatedate as date
from news as NW
left join newsclass as NC on NW.type =NC.id
left join ImgList as IG on NC.Name =IG.type and IG.title=NW.title
)as X
where X.type ='企业风采'
order by X.date desc
少了一个关联

rwh871212 2011-09-07
  • 打赏
  • 举报
回复

select top 4
X.Id,X.title,X.context,X.name,X.type,X.img,
X.updatedate ,X.date from
(
select distinct NW.Id,NW.title,NW.context,NW.updatedate as updatedate,NC.name,IG.type,IG.img ,IG.updatedate as date
from news as NW
left join newsclass as NC on NW.type =NC.id
left outer join ImgList as IG on NC.Name =IG.type
)as X
where X.type ='企业风采'
group by X.Id,X.title,X.context,X.name,X.type,X.img,X.updatedate ,X.date
order by X.updatedate,X.date desc

图片不同ID相同
rwh871212 2011-09-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 yan267 的回复:]
SQL code


select top 4 NW.Id,NW.type,NW.title,NW.context,NW.updatedate,NC.name
from news as NW
left join newsclass as NC on NW.type =NC.id
left join ImgList……
[/Quote]

我试过了也是不行
rwh871212 2011-09-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 liuchaolin 的回复:]
用left outer join
[/Quote]

试了,不行呀?
yan267 2011-09-07
  • 打赏
  • 举报
回复

select top 4 NW.Id,NW.type,NW.title,NW.context,NW.updatedate,NC.name
from news as NW
left join newsclass as NC on NW.type =NC.id
left join ImgList as IG on NC.Name =IG.type
where IG.type='企业风采'
GROUP BY NW.Id,NW.type,NW.title,NW.context,NW.updatedate,NC.name
order by NW.updatedate desc,IG.updatedate desc

md5e 2011-09-07
  • 打赏
  • 举报
回复
用left outer join

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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