Mysql query problem

Posted by Sergio on Stack Overflow See other posts from Stack Overflow or by Sergio
Published on 2010-03-12T13:22:19Z Indexed on 2010/03/12 13:37 UTC
Read the original article Hit count: 345

Filed under:
|

I have a problem with (for me to complicated) MySql query.

Okay, here is what I need to do:

First I need to check messages that some specific user received

$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'];

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:

$stat2=mysql_query("SELECT id, status FROM members WHERE id='".$whosend."' AND 
status ='1'")or die(mysql_error());
while ($s = mysql_fetch_array($stat))
{

Then my problems begin to show up.

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.

Is there any easier way to do this query? I tried with join tables like

$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' ....

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.

© Stack Overflow or respective owner

Related posts about mysql

Related posts about php