SQL计数未在PHP表上显示结果?

发布于 2025-02-13 18:34:06 字数 1196 浏览 0 评论 0原文

我的SQL表:

| user | bln | tipe |
|----- |-----| -----|
|  A   |  1  |  2   |
|  A   |  1  |  2   |
|  B   |  1  |  2   |
|  A   |  1  |  1   |
|  C   |  1  |  1   |
|  D   |  1  |  1   |

使用计数,在PHP中,我想列出这样的表:

| user |COUNT(tipe)|
|----- |-----------|
|  A   |  2  |
|  B   |  1  |
|  C   |  0  |
|  D   |  0  |
$sql = mysqli_query($link, "SELECT user, COUNT(tipe) FROM keg where bln=1 and tipe=2 GROUP BY user order by id asc;");
if(mysqli_num_rows($sql) == 0){
    echo '<tr><td colspan="8">Tidak ada data.</td></tr>';
}else{
    $no = 1;
    while($row = mysqli_fetch_assoc($sql)){
        echo '
        <tr>
            <td>'.$no.'</td>
            <td>'.$row['user'].'</td>
            <td>'.$row['tipe'].'</td>
        </tr>
        ';
        $no++;
    }
}
?>

但是表输出是这样的:

| user |COUNT(tipe)|
|----- |-----------|
|  A   |           |
|  B   |           |

我的问题是$ row = mysqli_fetch_assoc($ sql)吗?

我在SQL窗口中尝试了SQL,并且正常显示输出,但它不在PHP中。

My SQL table:

| user | bln | tipe |
|----- |-----| -----|
|  A   |  1  |  2   |
|  A   |  1  |  2   |
|  B   |  1  |  2   |
|  A   |  1  |  1   |
|  C   |  1  |  1   |
|  D   |  1  |  1   |

Using COUNT, in PHP I want to list the table like this:

| user |COUNT(tipe)|
|----- |-----------|
|  A   |  2  |
|  B   |  1  |
|  C   |  0  |
|  D   |  0  |
$sql = mysqli_query($link, "SELECT user, COUNT(tipe) FROM keg where bln=1 and tipe=2 GROUP BY user order by id asc;");
if(mysqli_num_rows($sql) == 0){
    echo '<tr><td colspan="8">Tidak ada data.</td></tr>';
}else{
    $no = 1;
    while($row = mysqli_fetch_assoc($sql)){
        echo '
        <tr>
            <td>'.$no.'</td>
            <td>'.$row['user'].'</td>
            <td>'.$row['tipe'].'</td>
        </tr>
        ';
        $no++;
    }
}
?>

But the table output is like this:

| user |COUNT(tipe)|
|----- |-----------|
|  A   |           |
|  B   |           |

Is my problem in $row = mysqli_fetch_assoc($sql)?

I tried the SQL in an SQL windows and it's showing the output normally, but it doesn't in PHP.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迎风吟唱 2025-02-20 18:34:06

这里有两个问题。首先,在PHP侧,您正在尝试在结果集中访问列Tipe,但是没有这样的列 - 它称为Count(Tipe)(Tipe)。这可以通过使用别名来解决。
其次,您的 子句删除所有没有bln = 1的行,而tipe = 2,因此您没有为用户C和D获得任何结果。
一种解决方案是不使用在其中子句,而是将条件移至case count> Count函数的表达式以进行评估:

SELECT   user, COUNT(CASE WHEN bln = 1 AND tipe = 2 THEN 1 END) AS tipe
FROM     keg 
GROUP BY user 
ORDER BY id ASC;

There are two issues here. First, from the PHP side, you're trying to access the column tipe in the result set, but there is not such column - it's called COUNT(tipe). This can be solved by using an alias.
Second, your where clause removes all the rows that don't have bln=1 and tipe=2, so you aren't getting any results for users C and D.
One solution would be to not use a where clause, but move the condition to a case expression for the count function to evaluate:

SELECT   user, COUNT(CASE WHEN bln = 1 AND tipe = 2 THEN 1 END) AS tipe
FROM     keg 
GROUP BY user 
ORDER BY id ASC;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文