title: How do I combine two queries on the same table to get a single result set in MySQL
body: <p>I am not very good at sql but I am getting there. I have searched stackoverflow but I can't seem to find the solution and I hope someone out there can help me. I have a table (users) with data like the following. The book_id column is a key to another table that contains a book the user is subscribed to.</p>
  
  <pre><code>|--------|---------------------|------------------|
  |   id   |      book_id        |       name       |
  |--------|---------------------|------------------|
  |   1    |         1           |        jim       |
  |   2    |         1           |       joyce      |
  |   3    |         1           |        mike      |
  |   4    |         1           |       eleven     |
  |   5    |         2           |        max       |
  |   6    |         2           |       dustin     |
  |   7    |         2           |       lucas      |
  |--------|---------------------|------------------|
  </code></pre>
  
  <p>I have a function in my PHP code that returns two random users from a specific book id (either 1 or 2). Query one returns the result in column 1 and result two returns the results in column 2 like:</p>
  
  <pre><code>|---------------------|------------------|
  |          1          |        2         |
  |---------------------|------------------|
  |        jim          |       max        |
  |       joyce         |     dustin       |
  |---------------------|------------------|
  </code></pre>
  
  <p>I have achieved this by running two separate queries as seen below. I want to know if it's possible to achieve this functionality with one query and how.</p>
  
  <pre><code>$random_users_with_book_id_1 = SELECT name FROM users WHERE book_id=1 LIMIT 2
  $random_users_with_book_id_2 = SELECT name FROM users WHERE book_id=2 LIMIT 2
  </code></pre>
  
  <p>Again, I apologise if it's too specific. The query below has been closest to what I was trying to achieve.:</p>
  
  <pre><code>SELECT a.name AS book_id_1, b.name AS book_id_2   
  FROM users a, users b 
  WHERE a.book_id=1 AND b.book_id = 2
  LIMIT 2
  </code></pre>
  
  <p>EDIT: I have created a fiddle to play around with his. I appreciate any help! Thank you!! <a href="http://sqlfiddle.com/#!9/7fcbca/1" rel="nofollow noreferrer">http://sqlfiddle.com/#!9/7fcbca/1</a></p>