简体   繁体   中英

errors with subquery when fetching from different tables

I ran the query below and had the error

sub-query returns multiple rows

I have replaced all the sub query in with any , and = as well. Yet the problem is not solved.

SELECT *
from UserPost
where UserId = UserId_Param
   or UserId = any (select UserId from UserPals
                    where PalUserId = UserId_Param and FriendStatus = 1)
   or UserId = any (select PalUserId from UserPals
                    where UserId = UserId_Param and FriendStatus = 1)
and privacy = 1
and PostId not in (select PostId from UserHidePost where UserId = UserId_Param)
and Committed = 1
and Trashed = 0 

union all

select *
from UserPost
where privacy = 2
  and PostId = any(select PostId from PostCategory
                   where PalCategoryId = any (select UserPalCategoryId
                                              from PalCategory
                                              where userId = UserId_Param
                                                 or PalUserId = UserId_Param))
  and PostId not in(select PostId from UserHidePost
                    where UserId = UserId_Param)
  and Committed = 1
  and (UserId = any (select UserId from UserPals
                     where PalUserId = UserId_Param and FriendStatus = 1)
       or (select PalUserId from UserPals
           where UserId = UserId_Param and FriendStatus = 1))
  and Trashed = 0
LIMIT lim_val OFFSET lim_offset;

The only obvious (to me) subquery that could cause this is:

 (select PalUserId from UserPals
       where UserId = UserId_Param and FriendStatus = 1)

Perhaps you want exists :

exists (select PalUserId from UserPals
        where UserId = UserId_Param and FriendStatus = 1
       )

As an aside, it seems a bit strange to mix = any () and in .

This subquery is missing an any clause:

SELECT *
from UserPost
where UserId = UserId_Param
   or UserId = any (select UserId from UserPals
                    where PalUserId = UserId_Param and FriendStatus = 1)
   or UserId = any (select PalUserId from UserPals
                    where UserId = UserId_Param and FriendStatus = 1)
and privacy = 1
and PostId not in (select PostId from UserHidePost where UserId = UserId_Param)
and Committed = 1
and Trashed = 0 

union all

select *
from UserPost
where privacy = 2
  and PostId = any(select PostId from PostCategory
                   where PalCategoryId = any (select UserPalCategoryId
                                              from PalCategory
                                              where userId = UserId_Param
                                                 or PalUserId = UserId_Param))
  and PostId not in(select PostId from UserHidePost
                    where UserId = UserId_Param)
  and Committed = 1
  and (UserId = any (select UserId from UserPals
                     where PalUserId = UserId_Param and FriendStatus = 1)
     ----THIS->  or (select PalUserId from UserPals
           where UserId = UserId_Param and FriendStatus = 1))-- <---
  and Trashed = 0
LIMIT lim_val OFFSET lim_offset;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM