0

好的,在学习如何从查询中获取值以根据查询中的值显示在不同的列中时,我真的挣扎了 3 天。这就是我想要的样子......

Name    0-30    30-60    60-90    90+
john            $50
june    $0.00
joe                               $100
fred                     $500

截至目前,我可以获得上面的名称和货币价值的列表,但我只知道如何显示它们如下......

Name    0-30    30-60    60-90    90+
john    $50     $50      $50      $50
june    $0.00   $0.00    $0.00    $0.00
joe     $100    $100     $100     $100
fred    $500    $500     $500     $500

我很简单地打印每列中的变量。任何帮助是极大的赞赏。

echo "<TABLE WIDTH='100%' BORDER='0' CELLSPACING='0' CELLPADDING='5'>
<TR>
<TD WIDTH='5%'><B>Job</B><BR></TD>
<TD WIDTH='30%'><B>Name</B><BR></TD>
<TD WIDTH='12%'><B>0-30 Days</B><BR></TD>
<TD WIDTH='12%'><B>30-60 Days</B><BR></TD>
<TD WIDTH='12%'><B>60-90 Days</B><BR></TD>
<TD WIDTH='12%'><B>90+ Days</B><BR></TD>
<TD WIDTH='15%'><B>Total Amount</B><BR></TD>
</TR><TR>";

SQL 查询在这里...... sqlfiddle。http://sqlfiddle.com/#!2/e5d9c/1/0

echo "<TD WIDTH='5%'>$primary_key</TD>
<TD WIDTH='30%'>$a_name</TD>
<TD WIDTH='12%'>$total / $timeperiod</TD>
<TD WIDTH='12%'>$total / $timeperiod</TD>
<TD WIDTH='12%'>$total / $timeperiod</TD>
<TD WIDTH='12%'>$total / $timeperiod</TD>
<TD WIDTH='15%'>$total / $timeperiod</TD>
</TR>";

我知道上面是错误的,但我不确定在上面放什么来显示我想要的示例?

4

3 回答 3

1

只需尝试在列中的 echo 之前添加一个条件。这应该看起来像:

`if ($res['name']>=0 and $res['name']<30) echo $res['data'];

if ($res['name']>=30 and $res['name']<60) echo $res['data'];

if ($res['name']>=60 and $res['name']<90) echo $res['data'];`

……

//$res['name'] --> john / june ... //$res['data'] --> $50 / $500 ...

我认为这是最简单的方法..

于 2013-01-18T09:27:29.463 回答
0

下面的代码片段将做必要的。

<?php

$query = "SELECT amount FROM mytable";

$res  = mysql_query($query);

while($data = mysql_fetch_row($res))
{
    echo "<tr>";
    if($data[0] < 30)
    {
        echo "<td colspan='4' align='left'>".$data[0]."</td>";
    }
    else
    {
        echo "<td>&nbsp;</td>";
        if($data[0] > 30 && $data[0] < 60)
            echo "<td colspan='3' align='left'>".$data[0]."</td>";
        else
        {
            echo "<td>&nbsp;</td>";
            if($data[0] > 60 && $data[0] < 90)
                echo "<td colspan='2' align='left'>".$data[0]."</td>";
            else
            {
                echo "<td>&nbsp;</td>";
                if($data[0] >  90)
                    echo "<td align='left'>".$data[0]."</td>";

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

?>
于 2013-01-18T09:31:23.627 回答
0
SELECT 
  Name,
  c0_30, c30_60, c60_90, c90
  FROM (                                   
    SELECT                                 
      Name,
      SUM(IF(val BETWEEN 0 AND 30, val, NULL)) As 'c0_30',
      SUM(IF(val BETWEEN 31 AND 60, val, NULL)) As 'c30_60',
      SUM(IF(val BETWEEN 61 AND 90, val, NULL)) As 'c60_90',
      SUM(IF(val > 90, val, NULL)) As 'c90'
    FROM talName
    GROUP BY Name WITH ROLLUP
  ) AS sums;

此查询将输出一个人的 col 的总值,val并根据其总值将它们​​分类到合适的列。

于 2013-01-18T09:36:29.497 回答