Nadeem Yousaf
Nadeem Yousaf

Reputation: 571

SQL Query to get different records from two tables

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

Answers (5)

Dilip Patel
Dilip Patel

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

Sunil Rajput
Sunil Rajput

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

Sankar V
Sankar V

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

Parth Chavda
Parth Chavda

Reputation: 1829

Using Sub Query

select user_id from `user` where user_id not in (select user_id from user_followers)

Upvotes: 0

RJParikh
RJParikh

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

Related Questions