php - codeigniter complex join only if rows don't exist -


i'm making dating application, tindler. users can or dislike other users, , if 2 users both each other option chat eachother. building query pull random profile , 1 of pictures @ random:

$data = $this->db ->select('users.id,display_name,city,state,gender,users_pictures.picture')   ->join('users_pictures','users_pictures.user_id = users.id')   ->order_by('id','random')   ->limit(1)   ->where(array('users.approved'=>1,'users_pictures.approved'=>1))   ->where(array('users.id !='=>$user_id))->get('users')->result_array();     

now here's i'm confused.. have table likes_dislikes consists of user_id, foreign_user_id, , event_type (like, dislike).

if disliked or liked user already, not want them in results. means can think of handling perform second query checks this, , 'random' query if have liked/disliked them see users haven't rated. there better way?

any appreciated :)

have heard of exists , not exists mysql commands? latter 1 should try.

select  u.id, display_name, city, state, gender, up.picture  users u inner join  users_pictures on up.user_id = u.id  not exists(     select       1           likes_dislikes          ld.foreign_user_id = u.id , ld.user_id = $loggedinuserid  ) 

the above list users, excluding liked/disliked users. trying form sql statement codeigniter without value being binded little hacky.

a brief investigation via google (which may outdated) suggests following should suitable.

//...rest of query $db->where("   not exists (     select 1      likes_dislikes      ld.foreign_user_id = u.id , ld.user_id = $loggedinuserid   ) , 1 = ", 1);  

take notice last part and 1 = ", 1);.


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -