如何将多列合并为一列,使其循环但按具有重复 id 的行与 PHP 和 OracleSQL 分开?

How to merge multiple column into one, make it looped but divided by row with duplicate id with PHP and OracleSQL?

我有代码,但我不能像我想要的那样制作它,要求是使我必须通过一列重复相同 id 拥有的数据的数据,尽管它与真实数据库分开(不同的列) ,所以这是我

的数据
number  pay_status  card_status
021998877  UNPAID  ACTIVE
021998875  NULL  NOT ACTIVE
021998875  NULL  ACTIVE

我想这样改:

number   others 
021998877  ACTIVE 
021998877  UNPAID  
021998876  NOT ACTIVE 
021998875  ACTIVE

现在我只能得到这样的输出:

021998877  ACTIVE UNPAID

这是我的输出代码

while ($row = oci_fetch_assoc($stid)) {
        printf("<tr><td>%d</td><td>%s</td></tr>\n", $row['no'],
            $row['card_status'],$row['pay_status'] );

我正在为数据库使用 PHP 和 OracleSQL。请帮忙,谢谢

所以我想你是说,如果 card_status 不是 NULL,那么打印第二行。

在那种情况下,这样做会非常简单。

while ($row = oci_fetch_assoc($stid)) {
    printf("<tr><td>%d</td><td>%s</td></tr>\n", 
                $row['no'],
                $row['pay_status'] );
    if ( $row['card_status'] != 'NULL' ) {
        printf("<tr><td>%d</td><td>%s</td></tr>\n", 
                $row['no'],
                $row['card_status'] );
    }
}

You may need to amend the test for NULL depending on what that value actually is on the result set!

从你的评论来看,这行中的 3 列似乎比你最初提到的要多。

要对结果集中的多个列执行此操作,您可以遍历结果集中的列并在输出中的多行上打印结果,将 NULL 转换为字符串 'NULL'.

while ($row = oci_fetch_assoc($stid)) {
    foreach ( $row as $colname => $value) {
        if ( $colname == 'no' ) {
            continue;   // ignore the key field column
        }

        $value = $value == NULL ? 'NULL : $value

        printf("<tr><td>%d</td><td>%s</td></tr>\n", 
                    $row['no'],
                    $value );
        }

    }
}