2012-08-28 45 views
0

以下查询有效,但由于某种原因,第一个select语句是唯一显示的URL。显示来自其他表格的项目,但是它们的URL是错误的。显示联合选择语句结果的问题

$sql = "(SELECT postsID as postsID, postsSubject AS postsSubject, postsTimestamp AS timestamp 
       FROM posts 
       WHERE postsCategory = '$id') 
      UNION 
       (SELECT eventID as eventID, eventTitle as eventTitle, eventsTimestamp as timestamp 
       FROM events 
       WHERE eventLocation = '$id') 
      ORDER BY timestamp DESC"; 

信息从事件和帖子表正确显示,但结果显示只来自帖子表。

例如,假设我有以下信息

postsID | postsSubject | postsTimestamp 
    1   post    123 

eventID | eventTitle | eventsTimestamp 
    2   event   456 

我有以下的,以显示我的结果

while($row = mysql_fetch_assoc($result)){ 
    ?> 
     <tr><td><? echo '<a href="viewevent.php?eventID=' . $row['eventID'] . '">' . $row['eventTitle'] . '</a>' ; ?></td> 
     <tr><td><? echo '<a href="viewpost.php?postID=' . $row['postsID'] . '">' . $row['postsSubject'] . '</a>' ; ?></td> 
    <? 
     if(preg_match('/[0-9]/',$row['timestamp'])){   
      list($yyyy,$dd,$mm) = explode('-',$row['timestamp']); 
        $newDate = $dd."-".$mm."-".$yyyy; 
      } 
     ?> 
      <td><center><? echo $newDate; ?></center></td></tr> 
     <? 
     } 
     echo '</table>'; 
} 

输出似乎是正确的

post 123 
event 456 

然而,两个结果链接到以下(分别)

viewpost.php?id = 1 
viewpost.php?id = 2 //this should be viewevent.php 
+0

添加表的模式。 –

回答

2

SQL:

$sql = "(SELECT postsID as ids, postsSubject AS description, postsTimestamp AS timestamp,'p' as status 
       FROM posts 
       WHERE postsCategory = '$id') 
      UNION 
       (SELECT eventID as ids, eventTitle as description, eventsTimestamp as timestamp, 'e' as status 
       FROM events 
       WHERE eventLocation = '$id') 
      ORDER BY timestamp DESC"; 

检索数据时,

while($row = mysql_fetch_assoc($result)){ 
    if ($row['status']=="e"){ 
    ?> 
     <tr><td><? echo '<a href="viewevent.php?eventID=' . $row['ids'] . '">' . $row['description'] . '</a>' ; ?></td> 
<? }else{?> 
     <tr><td><? echo '<a href="viewpost.php?postID=' . $row['ids'] . '">' . $row['description'] . '</a>' ; ?></td> 
<? } ?> 
    <? 
     if(preg_match('/[0-9]/',$row['timestamp'])){   
      list($yyyy,$dd,$mm) = explode('-',$row['timestamp']); 
        $newDate = $dd."-".$mm."-".$yyyy; 
      } 
     ?> 
      <td><center><? echo $newDate; ?></center></td></tr> 
     <? 
     } 
     echo '</table>'; 
} 
+0

只显示我帖子表中的值。但是,当我仅选择事件(删除查询的帖子部分)时,会显示事件。 – user1406951

+0

单独在mysql本身执行查询时,返回的结果是否准确? – sel

+0

嗯,我运行以下“选择eventID作为ids,eventTitle作为描述,eventsTimestamp作为时间戳,'e'作为状态 事件 WHERE eventLocation ='1' ORDER BY时间戳DESC”,它只是说“你的SQL查询已成功执行“,但实际上并未显示任何行。 – user1406951

0

联合/联合会使用第一个表中的列将所有行集中在一个表中。所以,结果集有一个PostsId,但没有EventsId。

也许你想要一个连接,它将并排放置列。也许你想把它作为两个单独的查询来运行。

这里是例子:

$sql = "select * 
     from (SELECT postsID as postsID, postsSubject AS postsSubject, postsTimestamp AS timestamp 
       FROM posts 
       WHERE postsCategory = '$id') p 
      cross join 
       (SELECT eventID as eventID, eventTitle as eventTitle, eventsTimestamp as timestamp 
       FROM events 
       WHERE eventLocation = '$id') e 
      ORDER BY postsTimeStamp DESC" 

请注意,这是生产笛卡尔乘积。所以,如果其中一个表中有更多的行,那么你会得到大量的行。如下

+0

我如何做一个连接作为两个单独的查询?你可以使用上面的信息让我看看吗? – user1406951