AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 203815
Accepted
Codex73
Codex73
Asked: 2018-04-13 16:38:50 +0800 CST2018-04-13 16:38:50 +0800 CST 2018-04-13 16:38:50 +0800 CST

查找具有相同选项的记录

  • 772

试图弄清楚如何找出哪些用户记录至少启用了 1 个类似选项,然后显示这些。

表:

user (id, username)
thing (id,title)
uthing (id, uid, tid)

Uthing表记录了每个用户选择了哪些选项。

uid = user.id
tid = thing.id

我需要将uthing用户 ID 1 的选项 ( ) 与所有其他用户进行比较。

如何找到具有uthingUSER ID 1 的 1 个或多个 options( ) 的用户,并使用找到的选项返回这些用户?

此查询将为我提供用户 ID 1 的所有选项。

select thing.title,thing.id as thingid 
from thing join uthing on uthing.tid = thing.id where uthing.uid = 1;
mysql relational-division
  • 2 2 个回答
  • 38 Views

2 个回答

  • Voted
  1. Best Answer
    DEarTh
    2018-04-13T22:00:53+08:002018-04-13T22:00:53+08:00

    您可以通过以下查询实现此目的

    select uthing.id, uthing.uid, thing.title
    from uthing left join thing on uthing.tid = thing.id
    where uthing.tid in (
            select thing.id
            from thing join uthing on uthing.tid = thing.id
            where uthing.uid = 1
         );
    
    • 2
  2. McNets
    2018-04-14T01:10:49+08:002018-04-14T01:10:49+08:00

    按照您的架构,我生成了下一个示例:

    create table user (id int, username varchar(100));
    create table thing (id int, title varchar(100));
    create table uthing (id int, uid int, tid int);
    
    insert into user values (1, 'user 1'),(2, 'user 2'),(3, 'user 3'),(4, 'user 4');
    insert into thing values (1, 'thing 1'),(2, 'thing 2'),(3, 'thing 3'),(4, 'thing 4'),(5, 'thing 5');
    insert into uthing values
    (1, 1, 1),(2, 1, 3),
    (3, 2, 2),(4, 2, 3),(5, 2, 5),
    (6, 3, 4),
    (7, 4, 1),(8, 4, 5);
    

    此查询选择id <> 1与 user 具有(存在)至少一个共同点的用户id = 1。

    select *
    from   user u
    join   uthing ut
    on     ut.uid = u.id
    join   thing t
    on     t.id = ut.tid
    where  u.id <> 1
    and    exists (select 1
                   from   uthing
                   where  uid = 1
                   and    tid = t.id);
    
    编号 | 用户名 | 编号 | uid | 潮 | 编号 | 标题  
    -: | :-------- | -: | --: | --: | -: | :------
     2 | 用户 2 | 4 | 2 | 3 | 3 | 事情 3
     4 | 用户 4 | 7 | 4 | 1 | 1 | 事情 1
    

    dbfiddle here

    • 1

相关问题

  • 是否有任何 MySQL 基准测试工具?[关闭]

  • 我在哪里可以找到mysql慢日志?

  • 如何优化大型数据库的 mysqldump?

  • 什么时候是使用 MariaDB 而不是 MySQL 的合适时机,为什么?

  • 组如何跟踪数据库架构更改?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve