Okay, now I understand. The master/detail form is what you want, unfortunately its not a single select statement. I do not know how to do this in a singe select statement (or even if thats possible).
URL of live demo of master/detail:
http://aspnet.4guysfromrolla.com/demos/MasterDetail.aspx
From the sounds of it, your two selects should probaby be;
SELECT * FROM authors ORDER BY name
SELECT * FROM books WHERE author_id=? ORDER BY name
Im still confused as to why you need DISTINCT. From the table design you showed me, authors should be unique to begin with.
If its sluggish then you need to change your design a little bit (or do some performance tuning on your database). And dont be rolling your eyes at me either, there shouldnt be any reason for two simple select statements to be sluggish.
Idea:
- Put authors in a drop down list. If there are tons of authors then you can seperate them further into "pages" of A-Z. User clicks B, the drop down fills up with all authors names starting with B.
- When the user selects an author from the drop down list, select the books that belong to that author into either a data grid or data list and display them.
- Be sure to use paging for the book list.
The layout should look something like this (which slightly resembles the output you showed me, just one author at a time)
Selected Author (Drop down list of authors)
(DataGrid or DataList of books of selected author)
Selected Author Book 1
Selected Author Book 2
Selected Author Book 3
etc...
Quick index tips to speed up queries:
- If you are going to select books and/or authors by name 90%+ of the time (ORDER BY name), then make the name column the clustered index for the table. This will improve performance for large selections drastically.
- Make sure you have a foreign key relationship with the author_id column in books to the author_id column in authors. This can improve performance for JOINs.
- Make sure the author_id and book_id are marked as primary keys.