A
A
Alexander Lalikin2014-03-14 15:25:08
SQL
Alexander Lalikin, 2014-03-14 15:25:08

How to modify SQL query with renaming of fields in tables joined via JOIN?

Removed books from tables for quick understanding -
books (id, name, author_id, genre_id)
authors - authors (id, name)
genres - genres (id_name)
query

SELECT * FROM `books` 
 JOIN `authors` ON (`books`.`author_id` = `authors`.`id`)
 JOIN `genres` ON (`books`.`genre_id` = `genres`.`id`)

returns the name of the genre in the `name` field, how to use aliases correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MinimaJack, 2014-03-14
@Lalikin

SELECT books.*, authors.name as aName, genres.name. as gName  FROM `books` 
 JOIN `authors` ON (`books`.`author_id` = `authors`.`id`)
 JOIN `genres` ON (`books`.`genre_id` = `genres`.`id`)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question