简体   繁体   English

如何描述要从联接表中选择的列

[英]How to describe columns to be selected from joined table

I have 2 tables: 我有2张桌子:

authors table: 作者表:

id_author
author(author name in this column)

author_ind table: author_ind表:

id_book
id_author

My query is: 我的查询是:

      SELECT author
        FROM authors
   LEFT JOIN author_ind 
          ON author_ind.id_book = author_ind.id_author
   LEFT JOIN authors author 
          ON author_ind.id_author = authors.id_author
       WHERE author_ind.id_book=%d

I need to get the authors info from the authors table based on the id_book from the author_ind table. 我需要根据author_ind表中的id_book从authors表中获取作者信息。 MYSQL error #1052 - Column 'author' in field list is ambiguous. MYSQL错误#1052-字段列表中的列“作者”不明确。

Little help please:) 请帮忙:)

Update: thank you everyone. 更新:谢谢大家。 correct query: 正确的查询:

SELECT authors.author
  FROM authors, author_ind
 WHERE author_ind.id_book =14
   AND authors.id_author = author_ind.id_author

14 is just a book id, I used it to test if it worked instead of %d 14只是一个书本ID,我用它来测试它是否有效而不是%d

Based on what you're trying to get, you've made this more complex than it needs to be. 根据您要获得的结果,您已经使它变得比所需的复杂。 You don't need to do a 2nd join on the authors table. 您无需在authors表上进行第二次联接。 Also, if you only want authors who have a record in the author_ind table, you can do a INNER JOIN instead of LEFT JOIN : 另外,如果只希望author_ind表中有记录的作者,则可以执行INNER JOIN而不是LEFT JOIN

SELECT author
FROM authors
INNER JOIN author_ind 
      ON authors.id_author = author_ind.id_author
WHERE author_ind.id_book=%d

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

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