你好我有两个MySQL Querys,我试图使用count和第一次选择条件的结果进行第二次选择。
这是我的代码:
$sql = "SELECT m.name, m.id
FROM members m
WHERE m.online_status = 1 {$dep} ORDER BY m.name ASC";
$sql_second = "SELECT time_management.clientid, clients.client,
SUM( CASE WHEN MONTH(`date`) = 1 AND taskid <> 1 AND YEAR(date)='$year' THEN TIME_TO_SEC( `time` ) AND user = (result from previouse slect) ELSE 0 END) AS (user_1)
FROM time_management
LEFT JOIN clients ON time_management.clientid = clients.id
WHERE {$dep_where} = {$department_var}
GROUP BY clientid";
因此,首先选择选择所有未知值的用户。姓名和身份证件
在第二个查询中,我需要的是选择例如:
假设第一个查询返回
id:1 name: Jon
id:2 name: Mike
id:3 name: Dave
总结果:3
所以在这种情况下,在第二个查询中,每个用户必须重复以下行3次:
SUM( CASE WHEN MONTH(`date`) = 1 AND taskid <> 1 AND YEAR(date)='$year' THEN TIME_TO_SEC( `time` ) AND user = (result from previouse slect) ELSE 0 END) AS (user_1)
其中,每行的用户为= 1,2,3,(user_1)将为(user_2),(user_3)
有没有办法直接在MySQL内部实现此操作?如果现在应该如何解决?