显示来自不同表格的时间

时间:2013-06-12 06:03:08

标签: php sql postgresql

我是网站的新手,希望有人能指导我走正确的道路。我正在使用PostgreSQL和php。现在我需要显示时间开始和结束时间。但由于某种原因,它显示不正确。我已经阅读了一些帮助指南来做到这一点,但在那方面不成功。我有两个包含以下数据的表格。

表1

index   product qualityno value

   1    Mangoes aaa1      10
   2    Bananas bbb2      10
   3    Apples  ccc3      10
   4    Orange  ddd4      10
   5    Grapes  eee5      10
   6    Melons  fff6      10
   7    Lemons  ggg7      11
   8    Berry   hhh8      11
   9    Strawberry iii9   11

表2

index   qualityno   Date    Start           End

   4    ddd4         04/08/13   10:05:00 AM 11:15:00 AM
   5    eee5         04/09/13   03:11:00 PM 04:25:00 PM
   6    fff6         05/20/13   01:15:00 PM 01:30:00 PM
   7    ggg7         05/20/13   08:15:00 AM 09:10:00 AM

我想在一个表中显示两个表信息,因此我在不对查询I进行更改的情况下进行了两次查询。

查询我

$sql1 =select * from table1 where value=10 or value=11
$result1 =pg_query($sql1);

查询II

$sql2 =select * from table2 t2
join table1 t1 on t1.index = t2.index
and t1.qualityno =t2.qualityno
$result2 =pg_query($sql2);

我在php中使用html结合两者:

while(($row1 =pg_fetch_array($result1)) && ($row2=pg_fetch_array($result2)))
{
echo "<tr>";
echo "<td> &nbsp; " . $row1["index"] . "</td>";
echo "<td> &nbsp; " . $row1["product"] . "</td>";
echo "<td> &nbsp; " . $row1["qualityno"] . "</td>";
echo "<td> &nbsp; " . $row1["value"] . "</td>";
echo "</tr>";
}

//I used the code below to insert the date, start and end time
//I'm hoping to print 0 if index number and qualityno no match  
//I echo  date first test my code if both index and qualityno existed.

 if(($row2[index] !=$row1[index]){ echo "<td>0</td>";}
 else{echo "<td> &nbsp; " . $row["Date"] . "</td><td>".$row2[start]."</td><td>".$row2[end]."</td>"; }

但是每次运行php查询都不匹配时,$ row2向上移动并占用index = 1并且不插入零'0'使得结果错误且索引错误。

index    product    qualityno   value
   1     Mangoes    aaa1    10

   4    04/08/13    10:05:00 AM 11:15:00 AM //wrong match

   2     Bananas    bbb2    10

   5    04/09/13    03:11:00 PM 04:25:00 PM //wrong match

   3    Apples  ccc3    10

   6    05/20/13    01:15:00 PM 01:30:00 PM //wrong match

   4    Orange  ddd4    10          

   5    Grapes  eee5    10

   6    Melons  fff6    10

   7    Lemons  ggg7    11

我希望我的桌子显示以下项目:

  index product    qualityno    value

   1    Mangoes      aaa1        10
          0       

   2    Bananas      bbb2        10
          0         

   3    Apples       ccc3        10
          0        

   4    Orange       ddd4        10
        04/08/13     10:05:00 AM    11:15:00 AM

   5    Grapes       eee5        10
        04/09/13     03:11:00 PM    04:25:00 PM

   6    Melons       fff6        10
        05/20/13     01:15:00 PM    01:30:00 PM

   7    Lemons       ggg7        11
         05/20/13    08:15:00 AM    09:10:00 AM

   8    Berry       hhh8         11
              0          

   9    Strawberry  iii9         11
               0         
//I was hoping that my code below can dispaly the list, 
//unfortunately its not successful

 //if(($row2[index] !=$row1[index]){ echo "<td>0</td>";}
 //else{echo "<td> &nbsp; " . $row["Date"] . "</td>"; }

如果我的描述令人困惑,请不要犹豫问我,请帮助解决这个问题,因为我已经困住了几天,我需要帮助。提前谢谢你。

1 个答案:

答案 0 :(得分:1)

SELECT t1.*, coalesce(Date,'0') AS Date,coalesce(Start,'') AS Start,
       coalesce(End,'') AS End
FROM table1 t1
LEFT JOIN table2 t2
  ON t1.index=t2.index AND t1.qualityno=t2.qualityno

或者如果你不介意得到空值,那么它甚至更简单:

SELECT t1.*, t2.Date,t2.Start,t2.End
       coalesce(End,'') AS End
FROM table1 t1
LEFT JOIN table2 t2
  ON t1.index=t2.index AND t1.qualityno=t2.qualityno

然后将结果转储到表格中。