如何从多个表到多个表中选择一对一的关系

时间:2016-03-07 12:48:21

标签: sql database relational-database

我有一个数据库,我可以保存书籍和作者。在我的模型中,一位作者可以拥有许多书籍,一本书可以由许多作者撰写。例如:

AUTHOR_ID|BOOK_ID
1|100
1|200
1|300
2|300
3|300
4|400

我试图找到只写一本书的作者,那本书必须只由该作者单独写。在上面的示例中,只有有效作者的AUTHOR_ID = 4 我需要编写一个选择来获取满足上述要求的作者ID,如何编写快速有效的选择来执行此操作?

2 个答案:

答案 0 :(得分:3)

select *
from BookAuthors t1
where not exists (select * from BookAuthors t2
                  where t2.BOOK_ID = t1.BOOK_ID
                    and t2.Author_ID <> t1.Author_ID)
  and not exists (select * from BookAuthors t3
                  where t3.Author_ID  = t1.Author_ID
                    and t3.BOOK_ID <> t1.BOOK_ID)

第一个NOT EXISTS用于确保同一个bookid没有第二位作者。

第二个NOT EXISTS是为了确保同一Author_ID没有写另一本书。

合并版本:

select *
from BookAuthors t1
where not exists (select * from BookAuthors t2
                  where (t2.BOOK_ID = t1.BOOK_ID
                         and t2.Author_ID <> t1.Author_ID)
                     or (t2.Author_ID  = t1.Author_ID
                         and t2.BOOK_ID <> t1.BOOK_ID))

答案 1 :(得分:0)

这是一个单独的答案,因为第一个是错误的。

如果您的数据库支持窗口函数,则一种方法是:

where

通过将条件移至join子句或select ba.* from BookAuthors ba where Author_ID in (select Author_Id from BookAuthors group by Author_Id having count(*) = 1) and Book_ID in (select Author_Id from BookAuthors group by Book_ID having count(*) = 1); 来执行此操作还有多种方法:

import {Component, Template, View, NgFor, bootstrap, CORE_DIRECTIVES, FORM_DIRECTIVES} from 'angular2/angular2';

@Component({
    selector: 'todo-app',
})

@View({
  directives : [NgFor, CORE_DIRECTIVES, FORM_DIRECTIVES]
})

@Template({
    url: 'components/todo-app/todo-app.html'
 })

class TodoAppComponent {
  todos : Array;
  constructor() {
    this.todos = [];

    TodoFactory.getAll().then((data) =>{
       this.todos = data; // I got JSON in todos object.
    });
  }
}
相关问题