我有两个表,users
和clients_closed_1
。
我需要按表client_closed_1
上的行数来计算结果,其中meeting = 1。
我是为time_closed
字段做的,但这很简单,因为没有条件。
它是搜索代码的一部分,因此我会向您展示所有内容。 使用此代码,我设法通过会议对其进行排序 - 但是没有会议= 1的行的用户不会从数据库中退出,即使他们没有开会也我需要他们显示。
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);
如果您需要更多代码/ doedn,请理解某些评论,我会解决它。
谢谢。
编辑:
$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
答案 0 :(得分:1)
我不确定我是否理解您正在寻找的100%标准,但这里是查询的粗略草稿:
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
这里发生的事情是,我们正在对子查询中的所有不同会议值进行计数,并根据"会议"的值将其连接到原始查询。对于每一行。
这给了我们总会议#34;按不同的会议值分组,不会删除行。其他2个子查询也是如此。
这也可以清除一些内容并允许我们只插入$order_criteria
,其中可以是time_closed count,meeting_count或status_count。只需设置默认值(id),以防用户不选择任何内容:)
编辑:我也建议尝试摆脱SELECT *
习惯。指定所需的列,输出会更好。当你开始处理更大的表时,它的效率也会高得多。
答案 1 :(得分:0)
在我写了一个很长的查询后,我找到了完美的解决方案。
SELECT SUM(IF(meeting='1' && user_c=users.ID, 1,0)) as meeting_count FROM clients_closed_1 JOIN users
此查询返回meeting_count
他们的值为' 1'的会议次数。
直到现在我还不知道我能做到这一点,所以我在这里分享了。我想这将来会有所帮助。