简体   繁体   English

左桌子上有特殊条件的左连接

[英]left join with special condition on right table

don't know if this is possible.. I'm using sqlite3 schema: 不知道这是否可能..我正在使用sqlite3模式:

CREATE TABLE docs (id integer primary key, name string); 创建表文档(ID整数主键,名称字符串);
CREATE TABLE revs (id integer primary key, doc_id integer, number integer); CREATE TABLE revs(id整数主键,doc_id整数,数字整数);

I want to select every job joined with only one of its revisions, the one with the highest number. 我要选择的每个工作都只有一个修订版本,编号最高。 How can I achieve this? 我该如何实现? Right now I'm doing a left join and getting everything and then I'm filtering it in the application, but this sucks.. 现在,我正在做一个左连接并得到所有内容,然后在应用程序中对其进行过滤,但这很糟糕。

(by the way, can you suggest me a good and easy introductory book on databases and how they work and maybe something about sql too..) thanks! (顺便说一句,您能为我推荐一本关于数据库以及它们如何工作以及关于sql的东西的简单易懂的入门书。)谢谢!

try this 尝试这个

   Select * From docs d
      Join revs r
         On r.doc_id = d.id
   Where r.number = 
         (Select Max(number ) from revs
          Where Doc_Id = d.Id)

or, if you want the docs with no Revs (Is this possible?) 或者,如果您想要没有修订的文档(这可能吗?)

   Select * From docs d
      Left Join revs r
         On r.doc_id = d.id
           And r.number = 
                (Select Max(number ) from revs
                 Where Doc_Id = d.Id)

Not sure if your engine supports this, but typically, you would do something like this in ANSI SQL: 不知道您的引擎是否支持此功能,但是通常,您会在ANSI SQL中执行以下操作:

SELECT docs.*
    ,revs.*
FROM docs
INNER /* LEFT works here also if you don't have revs */ JOIN revs
    ON docs.id = revs.doc_id
    AND revs.number IN (
        SELECT MAX(number)
        FROM revs
        WHERE doc_id = docs.id
    )

There are a number of ways to write equivalent queries, using common table expressions, correlated aggregate subqueries, etc. 有多种方法可以使用公用表表达式,相关的聚合子查询等编写等效查询。

select d.*, r.max_number
from docs d
left outer join (
    select doc_id, max(number) as max_number
    from revs
    group by doc_id
) r on d.id = r.doc_id

Database Design : Database Design for Mere Mortals by Hernandez 数据库设计:Hernandez的仅凡人数据库设计

SQL : The Practical SQL Handbook SQL: 实用SQL手册

If you want to hurt your head, any of the SQL books by Joe Celko . 如果您想伤脑袋,请参阅Joe Celko撰写的任何SQL书籍。

Here is a very good list of books for Database Design 这是有关数据库设计的很好的书籍清单

https://stackoverflow.com/search?q=database+book https://stackoverflow.com/search?q=database+book

If every job has revisions (eg, starting with rev 0), I would use the same approach as OrbMan, but with an inner join. 如果每个作业都进行了修订(例如,从修订版0开始),我将使用与OrbMan相同的方法,但使用内部联接。 (If you are certain you are looking for a 1-to-1 match. why not let SQL know, too?) (如果确定您要寻找一对一匹配。为什么也不要让SQL知道呢?)

select d.*, r.max_number
from docs d
inner join
(
    select doc_id, max(number) as max_number
    from revs
    group by doc_id
) r on d.id = r.doc_id

I'd recommend "A Sane Approach to Database Design" as an excellent introduction to good design practices. 我建议“数据库设计的Sane方法”作为对良好设计实践的出色介绍。 (I am slightly biased. I wrote it. But hey, so far it has a 5-star average review on Amazon, none of which reviews were contributed by me or any friends or relatives.) (我有点偏见。我写了它。但是,嘿,到目前为止,它在亚马逊上有五星级的平均评价,这些评价都不是由我或任何亲朋好友提供的。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM