title: Can MySQL update quantity field automatically?
body: <p>I'm creating a website using Laravel and MySQL. I have 2 MySQL tables: <em>posts</em> and <em>comments</em>. In <em>comments</em> table, I used a foreign key named <em>post_id</em> to refer to which post a comment belongs to. Now I want to keep track of the quantity of comments that belongs to each post, and I have 2 solutions:</p>
  
  <p>a). Put a <em>number_of_comments</em> field in posts table;</p>
  
  <p>b). Use <em>Select count(</em>)* to calculate the quantity.</p>
  
  <p>For solution a, I wonder if it is possible for MySQL to automatically update the quantity field in <em>posts</em> table each time I create/delete a comment or multiple comments. By saying automatically I mean MySQL would calculate the quantity by itself based on table relationship;</p>
  
  <p>For solution b, I wonder how I can do MySQL <em>orderBy</em> instruction, like ordering the post ascendingly/descendingly by the number of its comments.</p>
  
  <p>And which solution is better considering write less code and for better performance?</p>
  
  <p>Forgive me for being a MySQL noobie, any suggestion would be appreciated!</p>