SQL query for selecting foreign-key rows with more than one link to the primary-key table

Avi

I have a (simplified) scenario that goes something like this:

Document table:

   id   |    title   |  text
   ===========================
    1   |    Title1  |  "AAA"
    2   |    Title2  |  "BBB" 
    3   |    Title3  |  "CCC"

Document pictures

   id   |   doc_id    |  url
   ===================================================
    1   |    1       |  "http://some.domain.com/1.jpg"
    2   |    1       |  "http://some.domain.com/2.jpg"
    3   |    2       |  "http://some.domain.com/3.jpg"
    4   |    4       |  "http://some.domain.com/3.jpg"

Let's name these table documents and doc_pictures. I'm trying to create a query that will return all the documents that have more than one picture. In this example this means returning only the document with id 1.

Limitations and assumptions:

  1. Both of the tables are huge thus complex queries might take too long.
  2. I don't care how many foreign picture rows the document has. I only care that it's more than 1.
  3. I don't mind if the output is the id of the document or the row from the pictures table.
  4. I don't mind getting just a small subset of such documents (for example 10 documents that have more than 1 picture, and not all of the documents that have more than 1 picture)

The db is Mysql

Mikael Östberg

This can be used to get the document ids.

 select 
     doc_id,
     count(1)
 from 
     doc_pictures
 group by 
     doc_id
 having 
     count(1) > 1

And then you can use a where in on the documents table using those ids.

Something like this:

 select 
     *
 from
     documents
 where 
     id in (
        select 
            doc_id                
        from 
            doc_pictures
        group by 
            doc_id
        having 
            count(1) > 1
     )

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Relating two foreign key columns in Table A to a primary key column in Table B

来自分类Dev

SQL FOREIGN KEY的

来自分类Dev

Joining two tables with one table having a different foreign key

来自分类Dev

Query to convert a key-value table into a human-readable one?

来自分类Dev

two foreign keys to same primary key select statement MYSQL

来自分类Dev

ALTER TABLE语句与FOREIGN KEY约束冲突

来自分类Dev

SQL Server 违反 PRIMARY KEY 约束

来自分类Dev

SQL PRIMARY KEY重复错误,跨3个表的相同PRIMARY KEY

来自分类Dev

Why can I create a table with PRIMARY KEY on a nullable column?

来自分类Dev

SELECT with multiple PRIMARY KEY

来自分类Dev

Django Custom primary key

来自分类Dev

Cannot add same key to dictionary more than once

来自分类Dev

INSERT语句与FOREIGN KEY SAME TABLE约束冲突

来自分类Dev

MYSQL:基于SELECT结果的ALTER TABLE DROP FOREIGN KEY

来自分类Dev

EF 7:INSERT语句与FOREIGN KEY SAME TABLE冲突

来自分类Dev

带有FOREIGN KEY错误的ORACLE CREATE TABLE

来自分类Dev

EF迁移:ALTER TABLE语句与FOREIGN KEY约束冲突

来自分类Dev

如何修复:UPDATE 语句与 FOREIGN KEY SAME TABLE 约束冲突

来自分类Dev

与FOREIGN KEY约束冲突

来自分类Dev

filter by first foreign key

来自分类Dev

与FOREIGN KEY约束冲突

来自分类Dev

应该有class_name,foreign_key和primary_key的have_many

来自分类Dev

此 SQL 代码在添加 PRIMARY KEY 索引时的说明

来自分类Dev

SQL: CONSTRAINT [myPK] PRIMARY KEY [此处未指定] CLUSTERED

来自分类Dev

C#,SQL Server:INSERT语句与FOREIGN KEY约束冲突

来自分类Dev

两个 SQL Server 表之间的多个 FOREIGN KEY

来自分类Dev

INT PRIMARY KEY和INTEGER PRIMARY KEY SQLite之间的区别

来自分类Dev

Bulk insert with primary key Id

来自分类Dev

Oracle IDENTITY列与PRIMARY KEY

Related 相关文章

热门标签

归档