php - 从不同表获取时子查询出错

标签 php mysql sql

我运行下面的查询并出现错误

sub-query returns multiple rows

我已将所有子查询 in 替换为 any=。但问题还没有解决。

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;

最佳答案

唯一明显的(对我来说)可能导致这种情况的子查询是:

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

也许你想要存在:

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

顺便说一句,混合使用 = any ()in 似乎有点奇怪。

关于php - 从不同表获取时子查询出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31184416/

相关文章:

来自 Joel Spolsky 文章的 SQL 问题

php - 如何使用 Zend_Db 添加多行?

javascript - Bootstrap typeahead(自动完成)ajax返回多个值

PHP 和 MySQL 搜索

MySQL 从另一个表更新值

sql - 按日期相交方式加入 2 个表

php - Laravel 4 Eloquent 返回错误的 ID

javascript - 将 Array Php 转换为 JSON 时出错

Mysql 选择在一列中具有相同值而在另一列中具有不同值的行

MySQL JOIN 多个表不起作用