Reputation: 571
i have two tables in my DB,One is User and contains user_id, second is user_follower and this contain user_id and follower_id(user_id of other user). i want to get user list but don't want to get those user in it that is already in user_follower table for current user. for example i have user_id 1 and want to get user list for this, i want all user that is is not in user_followers as follower. can some one help me in this. please.
i tried
select user_id from user
MINUS
select user_id from user_followers
but i get error on this , it is not supported by msql
Upvotes: 3
Views: 285
Reputation: 822
You can use sub query in where clouse for filter data from multiple table
SELECT t.user_id from user t
WHERE NOT IN(SELECT uf.user_id FROM user_followers uf
WHERE uf.user_id = t.user_id);
Upvotes: 3
Reputation: 980
Try This Query, It Will help You
SELECT `user`.`user_id` FROM `user` WHERE `user`.`user_id` NOT IN( select `user_id` from `user_followers`)
Upvotes: 1
Reputation: 4128
Try the below query:
$users_list = $this->db->select("u.*",FALSE)
->from("user u")
->join("user_follower uf","u.user_id != uf.user_id")
->get();
//The query is:
//SELECT u.* FROM user u INNER JOIN user_follower uf ON (u.user_id != uf.user_id)
To view the results:
print_r($users_list->result_array());
Upvotes: 0
Reputation: 1829
Using Sub Query
select user_id from `user` where user_id not in (select user_id from user_followers)
Upvotes: 0
Reputation: 4166
Use JOIN
.
select U.user_id from user U
INNER JOIN user_followers UF ON U.user_id = UF.user_id
Upvotes: 0