繁体   English   中英

如何按用户级别(仅在过去一周内)从一列中总计“是”? 使用PHP和SQL

[英]How do I total up “yes's” from a column, by a userlevel and only for the past week? Using PHP and SQL

我希望有一个简单的问题,就是我什么都做不了。

基本上,我正在寻找一种方法来简单地计算从星期一开始的过去一周中用户级别=数字和用户名=用户名的每一列中的是。 也可以作为总计totaltot列中金额的一种方法。

我的SQL数据库的设置如下:

数据库名称 :1_1 表格: form_2

DB中的数据样本

username userlevel inspect signatu claimnum amounttot timestamp
-------- --------- ------- ------- -------- --------- ---------
BobS     7         yes     yes     kj98739  5388.00   2011-10-15 16:39:20
SteveN   8         yes     no      kj76739  5000.00   2011-10-15 15:29:00
TimK     7         no      yes     kj98734  6000.00   2011-10-15 14:35:06

如何列出每个团队的所有数据,我只需要在一个完全不同的表中得出低于此打印结果的总数,就可以将其放在下面。如何获取仅显示星期一开始的最近几周的数据:

<?php
$con = mysql_connect("localhost","1_1","XXXXXXXXXXXXXXXX");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("1_1", $con);

$result = mysql_query("SELECT * FROM form_2 WHERE userlevel = '5' ORDER BY timestamp DESC");

echo "<table border='1'>
<tr>
<th><font size='1'>Name</th>
<th><font size='1'>Latitude</th>
<th><font size='1'>Longatude</th>
<th><font size='1'>Inspect</th>
<th><font size='1'>Sig</th>
<th><font size='1'>Phone #</th>
<th><font size='1'>Insur</th>
<th><font size='1'>Claim</th>
<th><font size='1'>Appr</th>
<th><font size='1'>AdjusApp</th>
<th><font size='1'>Check</th>
<th><font size='1'>Amount</th>
<th><font size='1'>Time</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td><font size='1'>" . $row['username'] . "</font></td>";
  echo "<td><font size='1'>" . $row['lat'] . "</font></td>";
  echo "<td><font size='1'>" . $row['longa'] . "</font></td>";
  echo "<td><font size='1'>" . $row['inspect'] . "</font></td>";
  echo "<td><font size='1'>" . $row['signatu'] . "</font></td>";
  echo "<td><font size='1'>" . $row['gotphone'] . "</font></td>";
  echo "<td><font size='1'>" . $row['hain'] . "</font></td>";
  echo "<td><font size='1'>" . $row['claimnum'] . "</font></td>";
  echo "<td><font size='1'>" . $row['approved'] . "</font></td>";
  echo "<td><font size='1'>" . $row['adjustapprov'] . "</font></td>";
  echo "<td><font size='1'>" . $row['checkcollec'] . "</font></td>";
  echo "<td><font size='1'>" . $row['amounttot'] . "</font></td>";
  echo "<td><font size='1'>" . $row['timestamp'] . "</font></td>";

  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?> 

任何帮助将不胜感激。 谢谢。

这是一个建议:

SELECT userlevel, COUNT(*)
FROM form_2
WHERE timestamp BETWEEN x AND y
AND signatu = 'yes'
GROUP BY userlevel

“ x”和“ y”必须是界定查询的时间戳-它们可以由PHP提供,也可以直接在MySQL中查询。

编辑:固定查询,感谢@knittl。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM