从mysqli中的表中选择多个数据

时间:2016-09-14 09:06:45

标签: php mysql

我想从数据库中的单个表中检索多个数据,具有特定周的不同数据。参见下文:

<th>FLT No</th>
   <th>Fuel Type</th>
   <th>Capacity</th>
    <th>Ton Fuel-Mix</th>
    <th>Week1 Avg Utilization </th>
    <th>Remarks</th>
    <th>Week2 Avg Utilization </th>
    <th>Remarks</th>
    <th>Week3 Avg Utilization </th>
    <th>Remarks</th>
    <th>Week4 Avg Utilization </th>
    <th>Remarks</th>
    <th>Week5 Avg Utilization </th>
    <th>Remarks</th>

请参阅下面的查询:

$dd = date('Y-m-1'); $dd2  = date('Y-m-31');

$fd = mysqli_query($connection,"SELECT fltno,ago,ton,mix,(SELECT AVG(utilization) AS Ut1 FROM tab_fueltrans WHERE cast(refillDate as date) BETWEEN '$dd' AND '$dd2' AND weekno='week1'),control,
(SELECT AVG(utilization) AS Ut2 FROM tab_fueltrans WHERE cast(refillDate as date) BETWEEN '$dd' AND '$dd2' AND weekno='week2'),control,
(SELECT AVG(utilization) AS Ut3 FROM tab_fueltrans WHERE cast(refillDate as date) BETWEEN '$dd' AND '$dd2' AND weekno='week3'),control,
(SELECT AVG(utilization) AS Ut4 FROM tab_fueltrans WHERE cast(refillDate as date) BETWEEN '$dd' AND '$dd2' AND weekno='week4'),control,
(SELECT AVG(utilization) AS Ut5 FROM tab_fueltrans WHERE cast(refillDate as date) BETWEEN '$dd' AND '$dd2' AND weekno='week5'),control,
 FROM tab_fueltrans WHERE cast(refillDate as date) BETWEEN '$dd' AND '$dd2' GROUP BY fltno");


while($rw = mysqli_fetch_array($fd)){
$c++;?>
<tr>
     <td nowrap="nowrap"><?php echo $c;?></td>
      <td nowrap="nowrap"><?php echo $rw['fltno'];?></td>
    <td nowrap="nowrap"><?php echo $rw['ago'];?></td>
    <td nowrap="nowrap"><?php echo $rw['ton'];?></td>
   <td nowrap="nowrap"><?php echo $rw['mix'];?></td>
   <td nowrap="nowrap"><?php echo $rw['Ut1'];?></td>
    <td nowrap="nowrap"><?php echo $rw['control'];?></td>
     <td nowrap="nowrap"><?php echo $rw['Ut2'];?></td>
      <td nowrap="nowrap"><?php echo $rw['control'];?></td>
      <td nowrap="nowrap"><?php echo $rw['Ut3'];?></td>
      <td nowrap="nowrap"><?php echo $rw['control'];?></td>
      <td nowrap="nowrap"><?php echo $rw['Ut4'];?></td>
      <td nowrap="nowrap"><?php echo $rw['control'];?></td>
      <td nowrap="nowrap"><?php echo $rw['Ut5'];?></td>
      </tr>

<?php };}?>
</tbody>

上面取结果但显示那些平均列未知如下:未定义索引:Ut1,未定义索引:Ut2,未定义索引:Ut3,未定义索引:Ut4,未定义索引:Ut5。请问,我在做什么错误?任何指导/协助将不胜感激。

1 个答案:

答案 0 :(得分:1)

如果没有提供别名,MySQL将使用给定的列定义或表达式作为列别名。在您的情况下,它将使用您的子选择语句作为列别名。当它返回到您的脚本时,而不是您在子选择中使用的别名,它实际上将整个子选择字符串作为键。

这方面的一个例子是:

SELECT col1, col2 FROM tbl1 WHERE col1 > 10;

此查询的密钥为col1col2。但是,当我们更改此查询以包含表达式时:

SELECT AVG(col1), col2 FROM tbl1 WHERE col1 > 10;

我们现在获得了密钥AVG(col1)col2。这是因为MySQL使用表达式作为列键。如果我们更改此查询以使用列别名:

SELECT AVG(col1) AS col1avg, col2 FROM tbl1 WHERE col1 > 10;

我们应该将密钥设为col1avgcol2。当我们使用子选择来获取每行的列值时,这是相同的。如果我们不提供列别名,则子选择将成为我们的密钥,即使您在子选择中提供列别名,这也是相同的。使用表达式(函数,案例,子选择)时,最好的做法是为该列提供自己的别名。

在使用包含相同列名的多个表时,还应定义别名。常见的情况是id,它可以在许多表中很常见。这样可以避免任何歧义,并帮助您进行查询。

参考您的示例并使用子选择,您应该为所有子选择表达式提供唯一的别名。

SELECT
    col1,
    (SELECT AVG(value) FROM tbl2) AS tbl2avg,
    (SELECT MAX(value) AS max FROM tbl2) AS tbl2max,
    col2,
    col3 AS newCol
FROM
    tbl1
WHERE
    col1 > 10;

通过上述查询,我​​们应该获得密钥col1tbl2avgtbl2maxcol2newCol,因为它们都被定义为我们的列别名。