類似微博判斷使用者關係的sql語句,sql語句

來源:互聯網
上載者:User

類似微博判斷使用者關係的sql語句,sql語句

類似新浪微博的關注與相互關注 

不知道別人是怎麼設計的。

反正我是如下設計的

 ID   USER   FRIEND 

 1     A         B 

2      B         A

3     A         C

 ID為自增

 user為發起粉絲 friend為被粉絲

 現在需求如下。給出A、B兩使用者ID如何判斷A與B的關係 很簡單要,是A關注B,還是A與B相互關注 一條SQL儘可能的利用索引,不用OR,儘可能的快速 (不要求得到B是否關注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)
);

-- 查詢和A相互關注的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);
     
-- 查詢A關注,但未關注A的USER
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);

-- 查詢關注A,但A未關注的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);


sql if語句判斷

肯定是 一條記錄和一條記錄比吧
比如
declare @shla int ,@shlb int
select @shla=shl from a where 你的條件
select @shlb=shl from b where 你的條件

if(@shla>@shlb)
--你要執行的語句
else
--你要執行的語句
 
判斷使用者的sql語句的問題?

1.(正確)如果查詢的數大於0,即驗證成功
2.(正確)如果查詢的數大於0,即有重複註冊名
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.