如果其他表中不存在行,则Sql选择行

时间:2014-10-08 17:35:43

标签: sql

我有一个users表和一个likes表。用户可以随机显示其他用户的数据,并可以决定他是喜欢还是不喜欢它。我正在努力为他尚未评级的代理用户选择一个新的随机用户合作伙伴!

现在,我正在尝试选择likes表中没有行的所有用户,以及代理用户user与评分用户partner的关联评级。

users表是标准用户表,在likes我有列iduserpartnerrelation

我正在使用Laravel Eloquent,但也可以使用原始sql。

我的尝试:

// $oUser->id is the acting user
$oSearch = Db_User::
              select( 'db_users.*', 'db_likes.*' )
              ->where( 'db_users.id', '<>', $oUser->id )
              ->where( 'db_likes.user', '=', $oUser->id )
              ->where( 'db_likes.relation', '<>', 'dislike' )
              ->where( 'db_likes.relation', '<>', 'like' )
              ->where( 'db_likes.relation', '<>', 'maybe' )
              ->join( 'db_likes', 'db_users.id', '=', 'db_likes.partner' );

这是错误的,因为我没有通过此尝试选择任何新用户。我认为这是因为在likes中找不到任何行!当他尚未评级时没有行,所以没有结果。这是对的吗?

编辑:

$oSearch = Db_User::
              select( 'db_users.*' )
              ->where( 'db_users.id', '<>', $oUser->id )
              ->where( 'db_users.sex', '=', $strSex )
              ->whereRaw( "not exists ( select 1 from db_likes where db_likes.relation in ('dislike','like','maybe') and db_likes.user = " .$oUser->id . " and db_likes.partner = db_users.id )" );

错误:"{"error":{"type":"Illuminate\\Database\\QueryException","message":"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'IO8fMLYUPHfX1HrwkAWc2xqX' in 'where clause' (SQL: select db_users .* from db_users where db_users . id <> IO8fMLYUPHfX1HrwkAWc2xqX and db_users . sex = w and not exists ( select 1 from db_likes where db_likes.relation in ('dislike','like','maybe') and db_likes.user = IO8fMLYUPHfX1HrwkAWc2xqX and db_likes.partner = db_users.id ) order by RAND() limit 1)","file":"\/Applications\/MAMP\/htdocs\/adamundeva-server\/adamundeva\/vendor\/laravel\/framework\/src\/Illuminate\/Database\/Connection.php","line":625}}"

**

最终解决方案:

**

$oSearch = Db_User::
              select( 'db_users.*' )
              ->where( 'db_users.id', '<>', $oUser->id )
              ->where( 'db_users.sex', '=', $strSex )
              ->whereRaw( "not exists ( select 1 from db_likes where db_likes.relation in ('dislike','like','maybe') and db_likes.user = '" .$oUser->id . "' and db_likes.partner = db_users.id )" );

3 个答案:

答案 0 :(得分:2)

您可以使用not exists选择尚未与某位用户合作的所有用户

select * from
db_users dbu
where not exists (
    select 1 from db_likes dbl
    where dbl.relation in ('dislike','like','maybe') -- not sure if this is necessary
    and dbl.user = $oUser->id
    and dbl.partner = dbu.id
)

http://sqlfiddle.com/#!2/8c3bb9/6

答案 1 :(得分:1)

我认为你想要的sql是:

    select db_users.*, db_likes.* from db_users 
left join db_likes on db_users.id = db_likes.partner
        and db_likes.user =  'Sx12cltADam2XvzBMUMwq7DG'
        and db_likes.relation <> 'dislike'
        and db_likes.relation <> 'like'
         and db_likes.relation <> 'maybe'
where db_users.id != 'Sx12cltADam2XvzBMUMwq7DG'

你想要做的就是加入喜欢的表来吸引所有用户,即使他们不喜欢任何东西。

在Laravel中,它可能就像将连接更改为左连接一样简单:

   $oSearch = Db_User::
                  select( 'db_users.*', 'db_likes.*' )
                  ->where( 'db_users.id', '<>', $oUser->id )                 
                  ->leftjoin( 'db_likes', function($join)
                  {
                       $join->on('db_users.id', '=', 'db_likes.partner' )
                        ->on('db_likes.user', '=', $oUser->id )
                        ->on('db_likes.relation', '<>', 'dislike' )
                        ->on('db_likes.relation', '<>', 'like' )
                        ->on('db_likes.relation', '<>', 'maybe' )
                  }
                  );

答案 2 :(得分:1)

我不了解Laravel Eloquent,但在SQL中你可以使用where子句中的子查询过滤掉那些:

-- sample user id SxKJAhu7LRp8zj6nXZ5g0JRh has partner IO8fMLYUPHfX1HrwkAWc2xqX
-- that should not show up
SELECT * from db_users
WHERE id NOT IN (SELECT partner
        FROM db_likes
        WHERE user = 'SxKJAhu7LRp8zj6nXZ5g0JRh')
    AND id <> 'SxKJAhu7LRp8zj6nXZ5g0JRh'