类似微博判断用户关系的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);
时间: 2024-08-15 07:59:39

类似微博判断用户关系的sql语句的相关文章

【转】MySQL用户管理及SQL语句详解

[转]MySQL用户管理及SQL语句详解 1.1 MySQL用户管理 1.1.1 用户的定义 用户名+主机域 mysql> select user,host,password from mysql.user; +--------+------------+-------------------------------------------+ | user | host | password | +--------+------------+---------------------------

mysql用户管理, 常用sql语句,mysql数据库备份恢复

mysql用户管理 新创建一个指定IP的用户,可以访问mysql的某些库某些表. 所有库的所有表,如果想指定访问某个库某些表,只需要修改名称user1 指定用户名br/>@后面的ip是指定ip,%表示所有的ipindentified by 后面是用户的密码验证用用户user1登录也可以指定localhost,登录时不输入本机ip地址即可访问查看授权,用于授权给新用户,新权限: 常用sql 语句 查看库表的行数搜索:select count() from mysql.user;搜索:select

MySQL用户管理及SQL语句详解

1.1 MySQL用户管理 1.1.1 用户的定义 用户名+主机域 mysql> select user,host,password from mysql.user; +--------+------------+-------------------------------------------+ | user | host | password | +--------+------------+-------------------------------------------+ | r

1.1 MySQL用户管理及SQL语句详解

1.1 MySQL用户管理 1.1.1 用户的定义 用户名+主机域 mysql> select user,host,password from mysql.user; +--------+------------+-------------------------------------------+ | user | host | password | +--------+------------+-------------------------------------------+ | r

查看oracle 用户执行的sql语句历史记录

select * from v$sqlarea t order by t.LAST_ACTIVE_TIME desc 注意 :执行此语句等等一些相关的语句 必须具有DBA 的权限 虽然这条语句很普通 但是需要的时候很管用 能够及时查出一个人执行sql语句情况 -------oracle 查看已经执行过的sql 这些是存在共享池中的 --------->select * from v$sqlarea t order by t.LAST_ACTIVE_TIME desc -----------查看o

MySQL创建用户,常用SQL语句以及数据库备份与恢复

一.创建普通用户并授权 1.创建用户并授权 [[email protected] ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.36 MySQL Community Server (GPL) Copyright (c) 2000, 2017, Oracle a

ORACLE数据库SQL语句的执行过程

首先是最简单链接数据库的过程,JDBC标准的链接的过程: 1. 载入JDBC驱动程序,一般来说载入驱动程序,采用的都是利用Class.forName()反射来加载驱动 Oracle: Class.forName("oracle.jdbc.driver.OracleDriver"); SQLServer: Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); MySql: Class.forNam

Python 操作文件模拟SQL语句功能

Python操作文件模拟SQL语句功能 一.需求 当然此表你在文件存储时可以这样表示 1,Alex Li,22,13651054608,IT,2013-04-01 现需要对这个员工信息文件,实现增删改查操作 1. 可进行模糊查询,语法至少支持下面3种: 1. select name,age from staff_table where age > 22 2. select * from staff_table where dept = "IT" 3. select * from

Python操作文件模拟SQL语句功能

一.要求 当然此表你在文件存储时可以这样表示 1,li,22,18230393840,IT,2013-06-01 现需要对这个员工信息文件,实现增删改查操作 1. 可进行模糊查询,语法至少支持下面3种:     1. select name,age from staff_table where age > 22     2. select  * from staff_table where dept = "IT"     3. select  * from staff_table