Recommend friend SQL based on two-degree connections

Source: Internet
Author: User

Friend table structure

DROP TABLE IF EXISTS FRIEND;
CREATE TABLE Friend (
UID bigint not NULL comment ' User ID ',
Friend_uid bigint NOT NULL comment ' apply to friend's user ID ',
Sys_create_date datetime NOT NULL comment ' Application time ',
Sys_last_update datetime NOT NULL comment ' Update time ',
State tinyint not null comment ' friend status 0: Delete, 1: Request friend, 2: Friend ',
Primary KEY (UID, friend_uid)
) Engine=innodb DEFAULT Charset=utf8 COMMENT ' Friends table ';


Select Friend_uid from

Friend where state=2 and
UID in (select Friend_uid from friend where Uid=13 and state=2)

and Friend_uid not in (select Friend_uid from friend where uid=13) and friend_uid! = Limit 6


UID is the ID of the current user, and a user with ID 13 is queried for two-degree friends


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.