zoukankan      html  css  js  c++  java
  • 类似的微博推断客户关系sql声明

    类别似新浪微博的关注和共同关心 

    不知道别人是怎么设计的。

    反正我是例如以下设计的

     ID   USER   FRIEND 

     1     A         B 

    2      B         A

    3     A         C

     ID为自增

     user为发起关注者 friend为被关注者

     如今需求例如以下。

    给出A、B两用户ID怎样推断A与B的关系 非常easy要。是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);

    版权声明:本文博客原创文章。博客,未经同意,不得转载。

  • 相关阅读:
    docker 会这些也够
    Linux 学会这些基本可以啦
    xxxxxxxxx
    Angular
    mongo
    node
    git clone 解决Permission Denied (publickey)问题
    vue项目如何刷新当前页面
    vue——动态路由以及地址传参
    vue 单页应用点击某个链接,跳转到新页面的方式
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4625441.html
Copyright © 2011-2022 走看看