我有三个查询,我需要将它们合并为一个。有没有人知道怎么做?
$myTasks = "";
$query = "SELECT taskID_PK " .
"FROM tasks t " .
"LEFT JOIN task_assignee ta ON ta.taskID_FK = t.taskID_PK ".
"LEFT JOIN task_attachments a ON a.taskID_FK = t.taskID_PK ".
"LEFT JOIN employee e ON e.employeeID_PK = t.assignByID_FK ".
" WHERE TRUE AND isArchive != 1 AND taskStatus = 1 AND ta.clientID_FK = {$G_CLIENID} AND categoryID_FK !=0 group by taskID_PK ";
$RawData = db::select($query);
$myTasks= count($RawData);
$closeTasks = "";
$query = "SELECT taskID_PK,taskTitle,taskDescn,categoryID_FK,priority,date_format(createDate, '%d/%m/%Y') as createDate,ticketID_FK,
date_format(dueDate, '%d/%m/%Y') as dueDate ,assignByID_FK,createTime, taskStatus,closedDate,employeeName ,attachmentID_PK " .
"FROM tasks t " .
"LEFT JOIN task_assignee ta ON ta.taskID_FK = t.taskID_PK ".
"LEFT JOIN task_attachments a ON a.taskID_FK = t.taskID_PK ".
"LEFT JOIN employee e ON e.employeeID_PK = t.assignByID_FK ".
" WHERE TRUE AND isArchive != 1 AND (taskStatus != 1 ){$taskCreateClause} AND categoryID_FK !=0 group by taskID_PK ";
$RawData = db::select($query);
$closeTasks = count($RawData);
$workLoad ="";
$query = "SELECT taskID_PK,taskTitle,taskDescn,categoryID_FK,priority,date_format(createDate, '%m/%d/%Y') as createDate,ticketID_FK,
date_format(dueDate, '%m/%d/%Y') as dueDate ,assignByID_FK,createTime, taskStatus,closedDate,employeeName ,clientID_FK " .
"FROM task_assignee ta " .
"LEFT JOIN tasks t ON ta.taskID_FK = t.taskID_PK ".
"LEFT JOIN employee e ON e.employeeID_PK = ta.clientID_FK ".
" WHERE TRUE AND taskStatus = 1 ";
$RawData = db::select($query);
$workLoad = count($RawData);
答案 0 :(得分:0)
好的,现在我明白了这个问题,怎么样:
select a.taskID_PK,
(Select count(*)) .
"FROM tasks t " .
"LEFT JOIN task_assignee ta ON ta.taskID_FK = t.taskID_PK ".
"LEFT JOIN task_attachments a ON a.taskID_FK = t.taskID_PK ".
"LEFT JOIN employee e ON e.employeeID_PK = t.assignByID_FK ".
" WHERE TRUE AND isArchive != 1 AND taskStatus = 1 AND ta.clientID_FK = {$G_CLIENID} AND categoryID_FK !=0
and task_ID_PK = a.taskID_PK) as Count1,
group by taskID_PK
....
from tasks
选择主要ID,然后3个独立的选择语句返回一个计数并在开头加入ID。
答案 1 :(得分:0)
SELECT
(SELECT(COUNT(DISTINCT t.taskID_PK))
FROM tasks t
LEFT JOIN task_assignee ta ON ta.taskID_FK = t.taskID_PK
LEFT JOIN task_attachments a ON a.taskID_FK = t.taskID_PK
LEFT JOIN employee e ON e.employeeID_PK = t.assignByID_FK
WHERE TRUE AND isArchive != 1 AND taskStatus = 1 AND categoryID_FK !=0)as activetasks,
(SELECT(COUNT(DISTINCT t.taskID_PK))
FROM tasks t
LEFT JOIN task_assignee ta ON ta.taskID_FK = t.taskID_PK
LEFT JOIN employee e ON e.employeeID_PK = t.assignByID_FK
WHERE TRUE AND isArchive != 1 AND taskStatus = 1 AND ta.clientID_FK=1 AND categoryID_FK !=0)as mytasks,
(SELECT(COUNT(DISTINCT t.taskID_PK))
FROM tasks t
LEFT JOIN task_assignee ta ON ta.taskID_FK = t.taskID_PK
LEFT JOIN employee e ON e.employeeID_PK = t.assignByID_FK
WHERE TRUE AND isArchive != 1 AND taskStatus != 1 AND categoryID_FK !=0)as cloastasks,
(SELECT(COUNT(t.taskID_PK))
FROM task_assignee ta
LEFT JOIN tasks t ON ta.taskID_FK = t.taskID_PK
LEFT JOIN employee e ON e.employeeID_PK = ta.clientID_FK
WHERE TRUE AND taskStatus = 1)as workload,
(SELECT(COUNT(DISTINCT t.taskID_PK))
FROM tasks t
LEFT JOIN task_assignee ta ON ta.taskID_FK = t.taskID_PK
LEFT JOIN task_attachments a ON a.taskID_FK = t.taskID_PK
LEFT JOIN employee e ON e.employeeID_PK = t.assignByID_FK
WHERE TRUE AND isArchive = 1 AND categoryID_FK !=0)as archivetasks.
我结合了我的疑问,我想知道有没有更好的方法来结合这些来提高效率?