如何在asp.net中使用左连接获取单行 [英] how to get single row using left join in asp.net

查看:52
本文介绍了如何在asp.net中使用左连接获取单行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过在asp.net中使用左连接操作从table2获取图像路径。但是没有得到它..所以请帮助我..



这里我的查询是:

Hi, i want to get image path from table2 by using left join operation in asp.net .but didn't get it..so please help me..

here my query is:

SELECT n.newsid,n.state,n.district,n.region,n.views,n.currentplace,n.newstitle,n.description,n.poston,n.postby,ni.imagepath,ni.newsid 
FROM  tbl_userpost_news n 
LEFT JOIN tbl_userpost_newsimages ni ON n.newsid = ni.newsid 
AND (SELECT TOP 1 * FROM ni WHERE n.newsid=ni.newsid)  
Order By n.poston desc




请发送当前代码.......谢谢。



please send current code for that.......thank you.

推荐答案

如果你只想要第二个表中的图像列然后你可以使用子查询。

检查这个查询 -

If you want only the image column from the second table then you can use subquery to that.
Check this query-
SELECT n.newsid, n.state, n.district, n.region, n.views, n.currentplace, n.newstitle, n.description, n.poston, n.postby, (SELECT TOP 1 imagepath FROM tbl_userpost_newsimages ni WHERE n.newsid=ni.newsid) AS imagepath
FROM  tbl_userpost_news n
Order By n.poston desc





注意:我已从select li中删除了 ni.newsid st n.newsid 已经存在并且假设返回相同的值。



希望,它有帮助:)



Note: I have removed ni.newsid from select list as n.newsid is already there and suppose to return the same value.

Hope, it helps :)

< br>

检查左侧加入的前1名 [ ^ ]


您的AND条件正在应用于JOIN ... ON,因此它不匹配。

移动TOP 1位:

Your AND condition is being applied to the JOIN...ON, so it doesn't match properly.
Move the TOP 1 bit:
SELECT TOP 1  n.newsid,n.state,n.district,n.region,n.views,n.currentplace,n.newstitle,n.description,n.poston,n.postby,ni.imagepath,ni.newsid 
FROM  tbl_userpost_news n 
LEFT JOIN tbl_userpost_newsimages ni ON n.newsid = ni.newsid 
ORDER BY n.poston desc

它可以工作。


这篇关于如何在asp.net中使用左连接获取单行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆