简体   繁体   中英

Having trouble in PHP MySQL query

How we can do something as follows?

$results = $DB->query("SELECT * FROM users WHERE id!='{$id}' ");

Even I don't know to code,

$a = $DB->query("SELECT * FROM friends WHERE(someone is not friends with you)' ");

Columns in friends: user_id , friends_id

Use NOT IN

ID NOT IN (IDs OF FRIEND)

Example

$DB->query("SELECT * FROM user AS User WHERE User.id NOT IN (select * from friends where user_id = User.id)");
$DB->query("SELECT x.* FROM users x WHERE x.id NOT IN (myid, friendids)");

You need to get the persons whom are not your friends. right? So you have your user_id as $my_id . Then you can select the friend_ID 's from friends for whom the user_id field does not contain your user_id value($my_id). Then from those friend_ID's retrieved, you can select the users from Users table. Use this:

$DB->query(SELECT * FROM users WHERE id IN(select friend_id FROM friends WHERE user_id != {$my_id}))

Will work. :)

Not knowing the exact structure of your db makes your question a bit trick however going by the idea that someone is said to be your friend when he is your friend or you are his friend, you have to select those who you didn't add as friend and they as well didn't add you.

A query like this should help you.

"SELECT * FROM users WHERE users.id NOT IN (select * from friends where user_id != '{$your_id}' AND friend_id != '{$your_id}')"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM