title: MySQL subquery - Find only first record in a LEFT JOIN
body: <p>I'm trying to display a list of member records, and I have a few tables I'm using to display what I need.</p>
  
  <p>That's the easy part.  The part I need help with is with a table that has many records to each member record: Login history</p>
  
  <p>I want to display only the first row for each member record, that exists in the Login History table.  Alternatively, I may want to flip flop and display the last record in the Login History table, as well.</p>
  
  <p>here's what I've got so far:</p>
  
  <pre><code>SELECT m.memberid, m.membername, m.gender, mp.phone
  FROM tbl_members m, 
       tbl_members_phones mp, 
       tbl_members_addresses ma
  WHERE m.defaultphoneid = mp.phoneid
  AND m.defaultaddressid = ma.addressid
  </code></pre>
  
  <p>So that returns what's expected.</p>
  
  <p>The 2 columns from <code>tbl_members_login_history</code> I'd like to add to the returned result are: mh.<code>loggedtime</code>, mh.<code>ipaddy</code></p>
  
  <p>I know adding the <code>tbl_members_login_history</code> as a LEFT JOIN would return duplicates, so I'm thinking there must be a Subquery necessity here, in order to return just the 1st record for that <code>memberid</code> that exists in <code>tbl_members_login_history</code>. </p>
  
  <p>What I'm worried about is if no record in the history table exists, I still want to display that member info, but leave the history columns as NULL.</p>
  
  <p>Would this be a subquery incident?  and if so, how does one add that type of LIMIT? </p>