我有一个存储活动类型(swimming, running, soccer, tennis and basketball)under task_cstm
的数据库。
另一个数据库存储任务中存储的salesman name,date_start和date_due。
当我在phpMyAdmin
上运行时,
SELECT COUNT( tasks_cstm.activity_type_c ) FROM tasks_cstm
LEFT JOIN tasks ON tasks.id = tasks_cstm.id_c
where tasks_cstm.activity_type_c ="swimming" and assigned_user_id="abcdefg"
我得到了我想要的结果:因为它会为我计算游泳总数。
但是我将其放入 .PHP 文件中,该文件允许用户选择SALESMAN和DATE以查看销售人员在此期间未展示的活动数量:
<?php
$result0 = $GLOBALS['db']->query("SELECT tasks_cstm.activity_type_c,
COUNT( tasks_cstm.activity_type_c ) FROM tasks_cstm
LEFT JOIN tasks ON tasks.id = tasks_cstm.id_c
WHERE activity_type_c='swimming' AND date_start>='$st'
AND date_due<='$dt' AND assigned_user_id='$salesman' AND t.id=tc.id_c")
$result1 = $GLOBALS['db']->query("SELECT tasks_cstm.activity_type_c,
COUNT( tasks_cstm.activity_type_c ) FROM tasks_cstm
LEFT JOIN tasks ON tasks.id = tasks_cstm.id_c
WHERE activity_type_c='running' AND date_start>='$st'
AND date_due<='$dt' AND assigned_user_id='$salesman' AND t.id=tc.id_c")
$result2 = $GLOBALS['db']->query("SELECT tasks_cstm.activity_type_c,
COUNT( tasks_cstm.activity_type_c ) FROM tasks_cstm
LEFT JOIN tasks ON tasks.id = tasks_cstm.id_c
WHERE activity_type_c='tennis' AND date_start>='$st'
AND date_due<='$dt' AND assigned_user_id='$salesman' AND t.id=tc.id_c")
$result3 = $GLOBALS['db']->query("SELECT tasks_cstm.activity_type_c,
COUNT( tasks_cstm.activity_type_c ) FROM tasks_cstm
LEFT JOIN tasks ON tasks.id = tasks_cstm.id_c
WHERE activity_type_c='soccer' AND date_start>='$st'
AND date_due<='$dt' AND assigned_user_id='$salesman' AND t.id=tc.id_c")
$result4 = $GLOBALS['db']->query("SELECT tasks_cstm.activity_type_c,
COUNT( tasks_cstm.activity_type_c ) FROM tasks_cstm
LEFT JOIN tasks ON tasks.id = tasks_cstm.id_c
WHERE activity_type_c='basketball' AND date_start>='$st'
AND date_due<='$dt' AND assigned_user_id='$salesman' AND t.id=tc.id_c")
echo "<tr><td>" . $result0 . "</td><td>" . $result1 . "</td><td>" . $result2 . "</td><td>" . $result3 . "</td><td>" . $result4 . "</td></tr>";
有人可以帮助我并给我一些指导吗?
PS:由于我无法编辑我以前的帖子,我在这里发布。
谢谢。
答案 0 :(得分:0)
每次查询后都缺少半冒号。
$result2 = $GLOBALS['db']->query("SELECT tasks_cstm.activity_type_c,
COUNT( tasks_cstm.activity_type_c ) FROM tasks_cstm
LEFT JOIN tasks ON tasks.id = tasks_cstm.id_c
WHERE activity_type_c='tennis' AND date_start>='$st'
AND date_due<='$dt' AND assigned_user_id='$salesman' AND t.id=tc.id_c");
答案 1 :(得分:0)
我不知道你在那里有$ GLOBALS [&#39; db&#39;]对象。但是通常来说,方法query()仍然不会产生结果。你必须执行()它然后它会产生一个数组。然后迭代数组并回显值。
$query = $db->query('SQL select query goes here');
$result = $query->execute();
foreach($result as $s) {
echo $r[0];
echo $r[1];
...
}
答案 2 :(得分:0)
谢谢大家的帮助。我得到了我想要的结果。
我做的是添加 ... SELECT tasks_cstm.activity_type_c,COUNT(tasks_cstm.activity_type_c)每个结果的AS TOTALCOUNT(0-4)。
并且对于每个结果(0-4)我需要另外一个fetchbyassoc,所以我输入
$r0= $GLOBALS['db']->fetchByAssoc($result0);
$r1= $GLOBALS['db']->fetchByAssoc($result1);
$r2= $GLOBALS['db']->fetchByAssoc($result2);
$r3= $GLOBALS['db']->fetchByAssoc($result3);
$r4= $GLOBALS['db']->fetchByAssoc($result4);
我也回应了这个
echo "<tr>
<td>".$r0['totalCount0']."</td>
<td>".$r1['totalCount1']."</td>
<td>".$r2['totalCount2']."</td>
<td>".$r3['totalCount3']."</td>
<td>".$r4['totalCount4']."</td>
</tr>";
是的,我得到了我需要的答案。非常感谢所有UR帮助(^ 3 ^)