count和输出特定的单词位于数据库行中

mxg2im7a  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(239)
id | username | pass | reply         | date    |
 0  | user1    | MD5  | Access-Accept | date 1  | 
 1  | user2    | MD5  | Access-Reject | date 2  |
 2  | user3    | MD5  | Access-Reject | date 3  | 
 3  | user4    | MD5  | Access-Accept | date 4  |

试图找出如何计数和显示/输出表中的所有“access accept”和“access reject”。
这就是我被卡住的地方,当前的脚本正在计算所有的行,

<?php
 // start connection 
 $conn = new mysqli($servername, $username, $password, $dbname);
 $sql = 'SELECT * FROM table_1';
 if (mysqli_query($conn, $sql)) {
 echo "";
 } else {
 echo "Error: " . $sql . "<br>" . mysqli_error($conn);
 }
 $result = mysqli_query($conn,$sql);
 $totalsum = mysqli_num_rows($result);
 mysqli_close($con);
 ?>

还有html

<?php include 'count-words.php';?>
 <p>Total access: <?=$totalsum?> Total Sessions!</div></p>

继续使用社区帮助,注意脚本确实输出了我希望实现的目标。

mysql> SELECT reply, COUNT(reply) FROM radpostauth GROUP BY reply;
 +---------------+--------------+
 | reply         | COUNT(reply) |
 +---------------+--------------+
 | Access-Accept |          573 |
 | Access-Reject |          233 |
 +---------------+--------------+
 2 rows in set (0.00 sec)

因为php和sql不是我的强属性,所以我需要一些帮助来输出这两行的结果,比如:

<?=$accept?>  
 <?=$reject?>
nwlls2ji

nwlls2ji1#

SELECT reply,COUNT(reply) FROM table GROUP by reply

只是解释一下: count(reply) 以及 group by 提供回复列中每个值的计数。所以access accept和access reject将被计算在内,举个例子,您将收到:

Reply         Count(Reply)
Access-Accept       2
Access-Reject       2

正是你想要的。
你也可以 AS Reply Count 之后 COUNT(reply) 所以输出是reply count而不是count(reply)。

相关问题