-1

This is my code:

SELECT b.nameBook, a.firstName, a.lastName FROM books b 
   INNER JOIN books_author ba ON b.idBook = ba.BooksId 
   INNER JOIN author a ON a.idAuthor = ba.AuthorId 
   ORDER BY b.nameBook;

Result

I don't want to repeat the books name (in this case, "Fisica 11"). I want it like this. How can I do it? Sorry if this is a dumb question, i'm a beginner.

Also, I wanted to ask if there is any difference between the code above and the one below and which one is better to use:

SELECT nameBook, lastName, firstName 
    FROM books b, author a, books_author ba 
    WHERE ba.AuthorId = a.idAuthor 
        AND ba.BooksId = b.idBook

Both of them give the same result.

I tried GROUP BY but this is what happens.

I could easily fix this by using php but I want to use SQL. Thank you.

Chetan
  • 6,711
  • 3
  • 22
  • 32
dKrypt72
  • 26
  • 1
  • This is a formatting issue and is best handled at application level, not in sql. You can use union and ordering to get sg very similar to what you need, but that query is not going to be efficient. – Shadow Feb 25 '19 at 23:22
  • You can not retrieve the data in the same format as you want to display in the UI. You need to work around the data you get from the database to display in whatever format you want. On approach is to collect the data coming from database into a list of entities and then apply LINQ Group by on it to create the final set of entities which has one book with multiple authors and then display it. – Chetan Feb 25 '19 at 23:24
  • It is not clear that how are you using the data and how are you displaying the data in the UI so it is difficult to provide a proper solution. – Chetan Feb 25 '19 at 23:25
  • [This is the whole code](https://i.gyazo.com/2f10001f6a649498e2be9226f443aff5.png). As I said before, I can easily fix the output with PHP but I wonder if I can do it with SQL. – dKrypt72 Feb 25 '19 at 23:39

1 Answers1

-1

With your query, you always querying a SET of fields: author + book. There are no colspan/rowspan in sql output :)

Again, result is always table, like:

Author 1 | book 1

Author 1 | book 2

Author 2 | book 3

What are you expecting to see in the "author" colon?

Wingman
  • 47
  • 3