SQL statements and SQL statements similar to those used to determine user relationships on Weibo

Source: Internet
Author: User

SQL statements and SQL statements similar to those used to determine user relationships on Weibo

Similar to Sina Weibo's attention and mutual interest

I don't know how others design it.

I designed it as follows.

ID USER FRIEND

1 A B

2 B

3 A C

ID is auto-Increment

User is the initiator and friend is the victim.

The requirements are as follows. It is very easy to give the two user IDs A and B how to judge the relationship between A and B. Is it true that A pays attention to B, OR that A and B pay attention to an SQL statement and use the index as much as possible without OR, try to be as fast as possible (do not require B to be followed by)


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)
);

-- Query the users that are of mutual interest with.
Select *
From tb_user_concern
Where user = 'A'
And exists
(Select *
From tb_user_concern B
Where a. user = B. friend
And a. friend = B. user );

-- Query the USER of A but not the USER of
Select *
From tb_user_concern
Where user = 'A'
And not exists
(Select *
From tb_user_concern B
Where a. user = B. friend
And a. friend = B. user );

-- Query the users that follow A but not
Select *
From tb_user_concern
Where friend = 'A'
And not exists
(Select *
From tb_user_concern B
Where a. user = B. friend
And a. friend = B. user );


SQL if statement judgment

It must be a record and a record.
For example
Declare @ shla int, @ shlb int
Select @ shla = shl from a where your condition
Select @ shlb = shl from B where your condition

If (@ shla> @ shlb)
-- The statement you want to execute
Else
-- The statement you want to execute

How can I determine a user's SQL statement?

1. (correct) if the number of queries is greater than 0, verification is successful.
2. (correct) if the number of queries is greater than 0, there will be duplicate registration names

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.