C# 选择ASP.net的SQL联接查询
我是SQL新手,需要帮助构造此查询C# 选择ASP.net的SQL联接查询,c#,sql,asp.net,sql-server,C#,Sql,Asp.net,Sql Server,我是SQL新手,需要帮助构造此查询 SELECT User_Profile.ProfileId , User_Profile.FirstName , User_Profile.LastName , User_Profile.University , User_Profile.Programme , User_Profile.Picture , User_Friend.status , User_Profile.Aboutme
SELECT User_Profile.ProfileId
, User_Profile.FirstName
, User_Profile.LastName
, User_Profile.University
, User_Profile.Programme
, User_Profile.Picture
, User_Friend.status
, User_Profile.Aboutme
FROM User_Profile
FULL OUTER JOIN User_Friend ON
(
(User_Profile.ProfileId = User_Friend.ProfileId1)
AND (User_Friend.ProfileId = (SELECT ProfileId FROM User_Profile WHERE UserId = @UserId))
OR (User_Profile.ProfileId = User_Friend.ProfileId)
AND (User_Friend.ProfileId1 = (SELECT ProfileId FROM User_Profile WHERE UserId = @UserId))
)
WHERE (User_Profile.FirstName LIKE '%' + @FirstName + '%')
我认为问题在于从User_Profile中选择ProfileId,其中UserId=@UserId
谢谢你看来你的问题在这里: 用户_Friend.ProfileId从中选择ProfileId 我认为,这个on应该改为in或= 此外,您在此处缺少AND或or,似乎:
@LastName+'%'User\u Profile.ProfileId我可能完全误解了您的表架构,但在我看来,您确实在尝试这样做:
select
...
from User_Profile up
inner join User_Friend uf
on up.ProfileId = uf.ProfileId1 and uf.ProfileId = up.ProfileId
where up.UserId = @UserId and up.FirstName like '%' + @FirstName + '%'
union all
select
...
from User_Profile up
inner join User_Friend uf
on up.ProfileId = uf.ProfileId and uf.ProfileId1 = up.ProfileId
where up.UserId = @UserId and up.FirstName like '%' + @FirstName + '%'
在哪里。。。是您的列列表。基本上,我所做的是将复杂的连接条件分解为两个单独的查询,以帮助使事情更清楚
此外,我还从UserId=@UserId的User_Profile中获取了selectprofileid语句,并将其考虑在内。这就是我不确定您的数据的地方,我做出了一个可能不正确的假设。如果您将此代码缩进一点,我认为您的运气会更好。在第一个WHERE子句之后我迷路了。922字符行有点难理解read@Steve,您在查询中遇到的确切问题是什么?MYSQL中不存在完整的外部联接这是MYSQL还是SQL Server?您好,我试过了,但没有试过worked@Steve然后发布您得到的错误和表的架构。@Steve请参阅更新的答案。从语法上讲,这个查询非常混乱。谢谢,这个查询工作得很好,直到我包含=从User_Profile中选择ProfileId,其中UserId=@UserId,所以我猜这就是问题所在。我得到的错误消息是SQL执行错误。已执行选择。。。。。。。。。。。。CLR类型不存在,或者您没有访问该类型的权限