title: How do we use distinct in left join of mysql
body: <p>I am using PHP and MySQL for my application.</p>
  
  <p>I have a table "Enquiries" with PK enquiry_id and a table "Enquiry_comments" with FK enquiry_id. There will be multiple comments for each enquiry. In my join how do I show the last comment only against each enquiry?</p>
  
  <blockquote>
    <p>SELECT * FROM <code>enquiries</code> AS <code>e</code> LEFT JOIN <code>enquiry_comments</code> AS <code>ec</code>
    ON <code>ec</code>.<code>enquiry_id</code> = <code>e</code>.<code>enquiry_id</code> WHERE <code>e</code>.<code>status</code> = 'Active'
    ORDER BY <code>e</code>.<code>enquiry_id</code> DESC LIMIT 10</p>
  </blockquote>
  
  <p>The above query gives me duplicate records so I want to use <code>group by or distinct</code> on <code>enquiry_id</code> and <code>order by</code> <code>enquiry_comment_id DESC</code> for enquiry_comments and show only the recent comment with the listing.</p>
  
  <p><strong>The Table Structure:</strong></p>
  
  <p>Table Enquiries</p>
  
  <pre><code>enquiry_id  enquiry_about enquiry_date
  1           PHP           2017-09-20
  2           MySQL         2017-08-21
  </code></pre>
  
  <p>Table Enquiry_comments</p>
  
  <pre><code>enquiry_comment_id enquiry_id enquiry_comment enquiry_comment_date
  1                     1       PHP is Easy     2017-08-03
  2                     1       PHP is Ver 7    2017-09-17
  3                     2       MySQK is RDBS   2017-09-13
  </code></pre>
  
  <p>From the above example data I want to get the latest one comment with each enquiry so <code>PHP</code> should get one comment with <code>enquiry_comment_id 2</code> which is latest and <code>MySQL</code> get the <code>enquiry_comment_id 3</code></p>
  
  <p><strong><em>The Listing Data I want</em></strong></p>
  
  <pre><code>1   PHP    PHP is Ver 7    2017-09-17
  2   MySQL  MySQK is RDBS   2017-09-13
  </code></pre>