简体   繁体   English

MySQL内部联接在两列上

[英]MySQL inner join on two columns

I have two tables, books and authors . 我有两个桌子, booksauthors books has a author_id column and a secondary_author_id column (no books have more than two authors). books具有author_id列和secondary_author_id列(没有书籍具有两个以上的作者)。 I'm so far doing: 我到目前为止正在做:

SELECT * FROM books
LEFT JOIN authors
ON books.author_id=authors.id

which is handling the join with the first author. 正在处理与第一作者的加入。 I can't work out how I'd handle the secondary author though. 我无法弄清楚如何处理第二作者。 Should I change my schema, or do I just need a bit of SQL help? 我应该更改架构还是只需要一点SQL帮助?

SELECT       books.*, author1.*, author2.*
FROM         books
  LEFT JOIN  author AS author1
  ON         author1.author_id = books.author_id
  LEFT JOIN  author AS author2
  ON         author2.author_id = books.secondary_author_id

In SQL, you can alias the tables by adding it after the table name. 在SQL中,可以通过在表名后面添加表来对表进行别名。 Just be careful, now you'll have duplicate columns, so instead of author1.* you will probably want to alias the results of both author1 and author2. 请注意,现在您将拥有重复的列,因此您可能要对author1和author2的结果使用别名,而不是author1。*。

EDIT 编辑

Additional details -- Say you have your basic table (i'll include the details so if people want to test on their own they can): 其他详细信息-假设您有基本表格(我将提供详细信息,因此如果人们想自己进行测试,他们可以这样做):

CREATE DATABASE test;
USE test;

CREATE TABLE books
(
  book_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  title VARCHAR(50),
  author_id INT NOT NULL,
  secondary_author_id INT
);

CREATE TABLE authors
(
  author_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(50)
);

INSERT INTO authors (author_id,name) VALUES (1,'Sue Z. Que'),(2,'John Doe'),(3,'Bob Smith');
INSERT INTO books (book_id,title,author_id,secondary_author_id) VALUES (1,'JOIN-ing Two Tables',1,2);

If you do the select I mention above, your result will be the following: 如果您执行我上面提到的选择,您的结果将为:

|----------------------- books TABLE -----------------------------|---- authors table -----|---- authors table ---|
+---------+---------------------+-----------+---------------------+-----------+------------+-----------+----------+
| book_id | title               | author_id | secondary_author_id | author_id | name       | author_id | name     |
+---------+---------------------+-----------+---------------------+-----------+------------+-----------+----------+
|       1 | JOIN-ing Two Tables |         1 |                   2 |         1 | Sue Z. Que |         2 | John Doe |
+---------+---------------------+-----------+---------------------+-----------+------------+-----------+----------+

(I've added the top header just for calrity's sake) you see you have two author_id's and two name's (as they are joins of the same table and same column names). (为方便起见,我添加了顶部标题),您会看到有两个author_id和两个名称(因为它们是同一表和相同列名称的联接)。 BUT, if you alias the columns from the joins like so: 但是,如果您对连接中的列进行别名命名,如下所示:

SELECT       books.*, author1.name AS primary_author, author2.name AS secondary_author
FROM         books
  LEFT JOIN  authors AS author1
  ON         author1.author_id = books.author_id
  LEFT JOIN  authors AS author2
  ON         author2.author_id = books.secondary_author_id;

You get a much cleaner result: 您得到的结果更加干净:

|----------------------- books TABLE -----------------------------| authors table -|- authors table --|
+---------+---------------------+-----------+---------------------+----------------+------------------+
| book_id | title               | author_id | secondary_author_id | primary_author | secondary_author |
+---------+---------------------+-----------+---------------------+----------------+------------------+
|       1 | JOIN-ing Two Tables |         1 |                   2 | Sue Z. Que     | John Doe         |
+---------+---------------------+-----------+---------------------+----------------+------------------+
SELECT books.* FROM books, authors.name, secondary_authors.name
LEFT JOIN authors
ON books.author_id=authors.id
LEFT JOIN authors as secondary_authors
ON books.secondary_author_id=secondary_authors.id

You need to rethink your design, because one day there will be a book with three authors, and the next day there will be a book with zero. 您需要重新考虑设计,因为有一天一本书将由三位作者组成,而第二天有一本书将为零。 (I've been there myself.) (我自己去过那里。)

Edit 编辑

As your comment says: yes, you need a books_authors table. 正如您的评论所说:是的,您需要一个books_authors表。 As long as you have your indexes set up properly, it's not a big performance hit. 只要索引设置正确,对性能的影响就不会很大。

The most annoying part is that you're often going to want to string the authors together (one entry per book, concatenating all the authors into a single column). 最烦人的部分是您经常想要将作者串在一起(每本书一个条目,将所有作者串联在一个栏中)。 You'll probably end up creating a view for that. 您可能最终会为此创建一个视图。

只是在辅助ID上进行另一个连接

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

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