简体   繁体   English

如何在postgres中使用“ join”

[英]How to use “join” in postgres

I have two table, 我有两个桌子,

  1. authorCollection contains columns: author, key. authorCollection包含以下列:author,key。
  2. book contains columns: key, title, type,....etc. 本书包含以下几列:键,标题,类型等。

There are many types for book, I hope to select the type is not 'UNKNOWN' I hope to join these two table as following: 书籍有很多类型,我希望选择的类型不是'UNKNOWN',我希望将这两个表合并如下:

SELECT A.key, A.author,
       I.key, I."Type"
FROM   authorCollection AS A
JOIN   book AS I
ON  A.key = I.key AND I."TYPE" <> 'UNKNOWN';

But the result I get is: there are two column called "key", and author and type. 但我得到的结果是:有两列称为“键”,分别是作者和类型。 I hope there is just one "key" column for the result, because they are same. 我希望结果只有一个“关键”列,因为它们是相同的。

How can I fix this? 我怎样才能解决这个问题? I try "natural join", but not solved. 我尝试“自然加入”,但没有解决。 Thanks. 谢谢。

fixed above 固定在上面

based on this, I need to group by type and author: 基于此,我需要按类型和作者分组:

SELECT A.key, A.author, I."Type"
FROM authorCollection A JOIN
     book I
     ON  A.key = I.key AND I."TYPE" <> 'UNKNOWN'
GROUP BY A.author, I."Type";

And then I hope to find the author's name who only appear two times in different types book. 然后我希望找到在不同类型的书中只出现两次的作者姓名。 For example: 例如:

author    type
tom        1
tom        2
tom        3
alex       3
alex       3
tony       1
tony       1

The result is tony and alex, their book appears in two different types. 结果是托尼和亚历克斯,他们的书以两种不同的类型出现。 tom appears in three types, so it is not a result. tom出现三种类型,因此不是结果。 How can I write query statement to realize it? 如何编写查询语句来实现它? THANKS. 谢谢。

Just put the columns you want in the select . 只需将所需的列放在select You have two that are called KEY : 您有两个称为KEY

SELECT A.key, A.author, I."Type"
FROM authorCollection A JOIN
     book I
     ON  A.key = I.key AND I."TYPE" <> 'UNKNOWN';

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

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