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