title: Mysql query: How can I get the number of the users who sent messages to my specific user with status =1?
body: <p>I have a problem with (for me to complicated) MySql query.</p>
  
  <p>Okay, here is what I need to do:</p>
  
  <p>First I need to check messages that some specific user received</p>
  
  <pre><code>$mid=$_SESSION['user'];
  $stat1=mysql_query("SELECT id, fromid, toid, subject FROM messages WHERE 
  toid = '".$mid."'  AND subject != 'not readed' GROUP BY fromid ")
  or die(mysql_error());
  while ($h = mysql_fetch_array($stat1))
  {
  $whosend=$h['fromid'];
  </code></pre>
  
  <p>Second thing that I need to do is check the status of the users (deleted or not) who sent the messages ("fromid") to my specific user ("toid"). This I must do from another table:</p>
  
  <pre><code>$stat2=mysql_query("SELECT id, status FROM members WHERE id='".$whosend."' AND 
  status ='1'")or die(mysql_error());
  while ($s = mysql_fetch_array($stat))
  {
  </code></pre>
  
  <p>Then my problems begin to show up.</p>
  
  <p>How can I get the number of the users who sent messages to my specific user with status =1? Not the number of the messages but the total number of the users who sent them.</p>
  
  <p>Is there any easier way to do this query? I tried with join tables like</p>
  
  <pre><code>$stat=mysql_query("SELECT memebers.id, memebers.status, messages.toid, 
  messages.fromid,messages.subject,messages.id FROM members, messages
  WHERE messages.toid='".$mid."' AND members.status ='7' ....
  </code></pre>
  
  <p>But even in this query I need to have id's of the user who sent messages before this query so there will be another query before this join tables.</p>