I have a users
table and a likes
table. A user gets displayed a random other user's data and can decide whether he likes or dislikes it. I am struggling with selecting a new random user partner for the acting user that he has not yet rated!
Now, I am trying to select all users that do not have a row in the likes
table, with a relation rating of the acting user user
to the rated user partner
.
The users
table is a standard user table, in likes
I have the columns, id
, user
, partner
and relation
.
I am using Laravel Eloquent, but can also use raw sql.
My attempt:
// $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' );
It is wrong, because I do not get any new user selected with this attempt. I think it is because there can not be found any row in likes
! There are no rows when he has not yet rated, so there is no result. Is this correct?
EDIT:
$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: "{"error":{"type":"Illuminate\\Database\\QueryException","message":"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'IO8fMLYUPHfX1HrwkAWc2xqX' in 'where clause' (SQL: select
db_users.* from
db_userswhere
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}}"
**
FINAL SOLUTION:
**
$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 )" );