1

I have to 2 tables in my database. First one is author contains (int author_id, text name_of_author). Second one is book contains (int book_id, int author_id, text name_of_book, text genre). They related by author_id;

I'm trying to get a query, that will give me list of genre without duplicates, and number of authors who have written in this genre.

It should look something like this:

    GENRE  NAME_OF_AUTHERS
     novel       12
     poems       4
     fantasy     20 
     ...         ..
etc..

I try to do something like this:

SELECT DISTINCT A.GENRE, COUNT(B.NAME_OF_AUTHOR) FROM BOOK AS A
LEFT JOIN AUTHOR AS B
ON A.AUTHOR_ID = B.AUTHOR.ID;

But it gives me wrong result.

2
  • well...what results do you get? :) Commented Feb 28, 2014 at 11:26
  • 1
    @user3364458, i think your doing some mistake in table format, you are give a name like name_of_authers. But, i think no_of_authers. Commented Feb 28, 2014 at 11:41

2 Answers 2

1

If you just need count of authors for every genre, doesn't this simple query work?

mysql> select count(author_id), genre from boook group by genre;
Sign up to request clarification or add additional context in comments.

Comments

0

Try

SELECT 
    COUNT(A.NAME_OF_AUTHOR), 
    B.GENRE FROM AUTHOR AS A
LEFT OUTER JOIN 
    BOOK AS B 
ON 
    A.AUTHOR_ID = B.AUTHOR.ID
GROUP BY 
    B.GENRE;

If it is not working, publish the data model and the error or result you get from DB.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.