按条件排序

I have two tables, users and clients_closed_1.

I need to order the result by the count of the rows on the table client_closed_1 where meeting=1.

I did it for time_closed field but that was easy because there was no condition.

It's a part of a search code so I'll show you all of it. With this code I manage to order it by meeting - but users who has no rows with meeting=1 isn't pull out from the database and I need them to show even if they doesn't have meetings.

if (project_type($_GET['project']) == 1)    {
    $table = 'clients_closed_1';
}   else    {
    $table = 'clients_closed_2';
}
    $s_query = "SELECT *,COUNT(time_closed) as numc FROM `".$table."` FULL JOIN `users` ON users.ID=user_c WHERE 1=1";
    if (isset($_POST['search']))    {   
        if ($_POST['tm'] == 'da')   {
                $dd = strtotime($_POST['y']."-".$_POST['m']."-".$_POST['d']);
                $s_query = $s_query." && DATE(FROM_UNIXTIME(time_closed)) = DATE(FROM_UNIXTIME(".$dd."))";
        }   
        elseif ($_POST['tm'] == 'mon')  {
            $s_query = $s_query." && YEAR(FROM_UNIXTIME(time_closed))=".$_POST['y']." && MONTH(FROM_UNIXTIME(time_closed))=".$_POST['m'];
        }
        if (!empty($_POST['search_name']))  {
            $s_query = $s_query." && CONCAT(p_name,' ',l_name) LIKE '%".$_POST['search_name']."%'";
        }
        if (!empty($_POST['level']))    {
            $query = "&& (level=3 && project IN (SELECT `project` FROM `project` WHERE type='2')) || level=4";
        }
    }   else    {
        $s_query = $s_query." && YEAR(FROM_UNIXTIME(time_closed))=YEAR(NOW()) && MONTH(FROM_UNIXTIME(time_closed))=MONTH(NOW())";
    }

if (isset($_GET['order']))  {
    if ($_GET['order'] == 'closing')    {
        $s_query = $s_query." GROUP BY users.ID ORDER BY numc DESC";
    }
    elseif ($_GET['order'] == 'meeting')    {
        $s_query = $s_query." && meeting='1' GROUP BY users.ID ORDER BY numd DESC";
    }
}
$query = $db->query($s_query);

If you need any more code/doedn't understand something comment please and I'll fix it.

Thank you.

EDIT: example of $s_query:

   SELECT *,COUNT(time_closed) as numc, COUNT(meeting) as numd FROM `clients_closed_1`
   FULL JOIN `users` ON users.ID=user_c WHERE 1=1 && 
    YEAR(FROM_UNIXTIME(time_closed))=YEAR(NOW()) && 
    MONTH(FROM_UNIXTIME(time_closed))=MONTH(NOW()) 
    GROUP BY users.ID ORDER BY numc DESC

Im not sure I understand 100% of the criteria youre looking for but here is a rough draft of the query:

SELECT c.id, c.meeting, temp1.time_closed_count, temp2.meeting_count, temp3.status_count
FROM `clients_closed_1` c
FULL JOIN `users` u
ON c.user_c=u.ID 
LEFT JOIN (SELECT time_closed, count(time_closed) time_closed_count FROM clients_closed_1 GROUP BY time_closed) temp1
ON c.time_closed = temp1.time_closed
LEFT JOIN (SELECT meeting, count(meeting) meeting_count FROM clients_closed_1 GROUP BY meeting) temp2
ON c.meeting = temp2.meeting
LEFT JOIN (SELECT status, count(status) status_count FROM clients_closed_1 GROUP BY status) temp3
ON c.status = temp3.status
WHERE 1=1 
AND YEAR(FROM_UNIXTIME(time_closed))=YEAR(NOW()) 
AND MONTH(FROM_UNIXTIME(time_closed))=MONTH(NOW()) 
ORDER BY {$order_criteria} DESC

Whats happeneing here is, we are doing the count of all distinct meeting values in a subquery and joining it to the original query based on the value of "meeting" for each row.

This gives us the total "meetings" grouped by distinct meeting values, without cutting out rows. Such is the same for the other 2 subqueries.

This also cleans things up a bit and allows us to just insert the $order_criteria, where that could be time_closed count, meeting_count, or status_count. Just set a default (id) in case your user does not choose anything :)

Edit: Id also recommend trying to get out of the SELECT * habit. Specify the columns you need and your output will be much nicer. Its also far more efficient when you start dealing with larger tables.

After I wrote a really long query to do this I found the perfect soulution.

SELECT SUM(IF(meeting='1' && user_c=users.ID, 1,0)) as meeting_count FROM clients_closed_1 JOIN users

This query return as meeting_count the number of meeting which their value is '1'.

I didn't know I can do such thing until now, so I shared it here. I guess it can be helpull in the future.