0

我是该网站的新手,希望有人能引导我走上正确的道路。我正在使用 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);

查询二

$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 向上移动并占据索引 = 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>"; }

如果我的描述令人困惑,请毫不犹豫地问我,请帮助解决这个问题,因为我已经困了几天了,我需要帮助。先感谢您。

4

1 回答 1

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

然后将结果转储到表中。

于 2013-06-12T06:10:35.117 回答