简体   繁体   English

显示union select语句的结果时出错

[英]trouble displaying results of union select statement

The following query works, but for some reason the first select statement is the only URL being displayed. 以下查询有效,但由于某种原因,第一个select语句是唯一显示的URL。 Items from other tables are displayed, however, their URL is wrong. 显示其他表中的项目,但是它们的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";

The information is being correctly displayed from both the events and posts table, but the results are appearing to ONLY come from the posts table. 从事件和帖子表中正确显示信息,但结果只显示在posts表中。

For example, say I have the following info 例如,说我有以下信息

postsID |  postsSubject |  postsTimestamp
  1            post             123

eventID |  eventTitle   | eventsTimestamp
  2           event            456

I have the following to display my results 我有以下内容来显示我的结果

   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>';
}

The output appears to be correct 输出似乎是正确的

post   123
event  456

However, both results link to the following (respectively) 但是,两个结果都链接到以下(分别)

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

sql as below: 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";

when retrieving data, 检索数据时

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>';
}

A union/union all brings the rows together in one table, using the columns from the first table. union / union都使用第一个表中的列将行组合在一个表中。 So, the result set has a PostsId, but no EventsId. 因此,结果集具有PostsId,但没有EventsId。

Perhaps you want a join, which will put the columns side-by-side. 也许你想要一个连接,它将并排放置列。 Perhaps you want to run this as two separate queries. 也许您希望将其作为两个单独的查询运行。

Here is the example: 这是一个例子:

$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"

Do note that this is producing a cartesian product. 请注意,这是生产笛卡尔积。 So, if there are more rows from one of the tables, then you will get a proliferation of rows. 因此,如果其中一个表中有更多行,那么您将获得大量行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM