title: How can I use a foreign key and joins to get the info I need from mysql database
body: <p>I am currently achieving this with several steps, but it would be better if I could find what I need with one query. I think it's possible with joins and a foreign key, but I can't really figure out how it all works together. </p>
  
  <p>Anyway, I have two tables <code>doctors</code> and <code>appointments</code>, one column linked with a foreign key (fk is <code>doctor_id</code> in appointments referencing <code>id</code> in <code>doctors</code>) and I need to get an appointment date, within a certain range of time and link it by the <code>doctor_id</code> to the matching id in <code>doctors</code> table and get the name and phone number from that table as well. </p>
  
  <p>Here is the join statement I came up with:</p>
  
  <pre><code>SELECT a.doctor_id, a.appointment_date, b.last_name, b.preferred_phone 
  FROM appointments a,doctors b 
  WHERE a.appointment_date BETWEEN NOW() AND NOW() + INTERVAL 3 DAY
  </code></pre>
  
  <p>Which gives me all the doctors and says their ids are all 3, which is not the case (it's an auto incrementing column), but it is the doctor <code>id</code> linked to the appointment that I need to match with. So how can I achieve that with a foreign key? </p>