0

我有一个查询,它返回活动和非活动帖子的数量,以及特定的帖子详细信息。这是查询

$fetch = mysql_query("SELECT SQL_CALC_FOUND_ROWS * FROM
(SELECT DISTINCT propertyID, streetAddress, city FROM residence.property
WHERE residence.contact.contactEmailAddress1 ='$contactEmailAddress1' AND activePosting = '0') inactposts,
    (SELECT FOUND_ROWS() AS 'inactiveCount') inact,
(SELECT DISTINCT propertyID, streetAddress, city FROM residence.property
INNER JOIN contact ON residence.contact.ContactID = residence.property.ContactID
WHERE residence.contact.contactEmailAddress1 ='$contactEmailAddress1' AND activePosting = '1') actposts,
    (SELECT FOUND_ROWS() AS 'activeCount') act;")
or die('<li class=error>Ooops</li>'.mysql_error());

该查询在我的网页中有效。在 MySQL Workbench 中,它返回如下内容:

propID   address        city        inactCount  propID  address     city        actCount
366   110 Main Street   Perinton        1       352    3 Some Place Perinton        3
366   110 Main Street   Perinton        1       353    5 Joe Place  Perinton        3       
366   110 Main Street   Perinton        1       356    111 Main     Perinton        3

对我来说问题是子查询结果的 2 部分没有标识符(我可以找到),所以在 PHP 服务器构造的 HTML 中,我无法将列表分成非活动帖子(inactposts ) 和活动帖子 (actposts)。目前 HTML返回活动帖子的列表。我尝试将临时表的名称“inactposts”放在各个地方,但我只是收到错误消息

这是进行 HTML 构造的 PHP:

if( mysql_num_rows($fetch) ) {
while ($row = mysql_fetch_array($fetch)) {
    $inactiveCount = $row["inactiveCount"];
    $activeCount = $row["activeCount"];
    $storedStreetAddress = $row["streetAddress"];
    $storedCity3 = $row['city3'];
    $error_NumberInactives = "<ul>You have $activeCount active posts and $inactiveCount inactive posts.</ul>";
    $error_List_Actives = "<li>Your $activeCount active posts are:</li>";
    $errorMessages[0] = $error_NumberInactives;
    $errorMessages[1] = $error_List_Actives;
    $errorMessages[] = "<li> $storedStreetAddress, $storedCity3 </li>";
}else{ //error message}

如何从此查询构造单独的子查询结果列表?还是我需要不同的查询?

4

1 回答 1

0

如果您想要单独的行,您将需要另一个没有聚合的查询。或者,在一个查询中选择所有帖子(带有详细信息)并在 php 中解析结果以计算活动和非活动的数量:

SELECT DISTINCT propertyID, streetAddress, city, activePosting FROM residence.property
INNER JOIN contact ON residence.contact.ContactID = residence.property.ContactID
WHERE residence.contact.contactEmailAddress1 ='$contactEmailAddress1'

ps 我假设您已经在提供的代码之外清理了您的输入,否则您需要这样做。

于 2013-06-18T19:24:24.977 回答