title: Query to select random values with inner join on three tables
body: <p>I have a database with tree tables,</p>
  
  <pre><code>person: id, bio, name
  book: id, id_person, title, info
  file: id, id_book, location
  </code></pre>
  
  <p>Other information: Book is about ~50,000 rows, File is about ~ 300,000 rows.</p>
  
  <p>What I'm trying to do is to <code>select</code> 12 different authors and <code>select</code> just one <code>book</code> and from that <code>book</code> select <code>location</code> from the table <code>file</code>.</p>
  
  <p>What I tried is the following: </p>
  
  <pre><code>SELECT DISTINCT(`person`.`id`), `person`.`name`, `book`.`id`, `book`.`title`, `book`.`info`, `file`.`location`
  FROM `person`
  INNER JOIN `book`
  ON `book`.`id_person` = `person`.`id`
  INNER JOIN `file`
  ON `file`.`id_book` = `book`.`id`
  LIMIT 12
  </code></pre>
  
  <p>I have learned that the DISTINCT does not work the way one might expect. Or is it me that I'm missing something? The above code returns books from the same author and goes with the next one. Which is NOT what I want. I want 1 book from each one of the 12 different authors. </p>
  
  <p>What would be the correct way to retrieve this information from the database?  Also, I would want to retrieve 12 <strong>random</strong> people. Not people that are stored in consecutive order in the database,. I could not formulate any <code>query</code> with <code>rand()</code> since I couldn't even get different authors. </p>
  
  <p>I use MariaDB. And I would appreciate any help, especially help that allows to me do this with great performance.</p>