Similar microblog inference customer relationship SQL statement

Source: Internet
Author: User

Categories like Sina Weibo's attention and common concern

Do not know how others are designed.

Anyway I was designed for example the following

ID USER FRIEND

1 A B

2 B A

3 A C

ID is self-increment

User is the person who initiated the followers friend as a concern

Today's needs such as the following.

Give a, b two user ID how to infer the relationship between A and b very easy to. is a concern B. Or a with B mutual attention a SQL as far as possible the use of the index, without or, as high as possible (do not ask B to pay attention to a)


SELECT *
From xxxxx
where user= ' A ' and friend= ' B '
Union All
SELECT *
From xxxxx
where user= ' B ' and friend= ' A '


CREATE TABLE Tb_user_concern
(ID int Auto_increment primary KEY,
USER varchar (32),
FRIEND varchar (32)
);

--queries and a mutual attention of the user
SELECT *
From Tb_user_concern A
where user = ' A '
and exists
(SELECT *
From Tb_user_concern b
where A.user = B.friend
and a.friend = B.user);

--Query a concerned, but not concerned about the user of a
SELECT *
From Tb_user_concern A
where user = ' A '
And NOT EXISTS
(SELECT *
From Tb_user_concern b
where A.user = B.friend
and a.friend = B.user);

--query attention a, but a not concerned about the user
SELECT *
From Tb_user_concern A
where friend = ' A '
And NOT EXISTS
(SELECT *
From Tb_user_concern b
where A.user = B.friend
and a.friend = B.user);

Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

Similar microblog inference customer relationship SQL statement

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.