输出这两个表的结果

时间:2011-10-06 01:54:42

标签: php mysql syntax-error

更新的SQL

SELECT listTitle, listLength, listCmt, listDt,mBCFName, mBCLName, moAmt
FROM User U
INNER JOIN Listing L on (U.uID = L.uID)
INNER JOIN MerchantOffer MO ON (L.listID = MO.listID)
INNER JOIN Merchant M on (M.mID = MO.mId) 
GROUP BY listTitle
ORDER BY listDt DESC
LIMIT 0,5

稍微更新一下PHP

    <?php 
    $result = $sth1->fetchAll(PDO::FETCH_ASSOC);
    require_once('inc/php/timeAgo.php');
    echo $then;
    foreach($result as $row)
    { 
    echo "<div class='listing'>";
        print '<br>Title: ' . $row['listTitle'] . '<br>Comment: ' . $row['listCmt'] . 
        '<br><br>' . $days . ' days ' . $hours . ' hours ago' . '<br><br>' . '<br>Offer By: ' . $row['mBCFName']. ' ' . $row['mBCLName'] . '<br> for: ' . $row['moAmt'];
    echo "</div>";
    }
    unset($sth1);   
    ?>  

提供如下输出: http://i.stack.imgur.com/pNfU5.png

它应该输出......例如:

Title: Apple iPhone 4S
Comment: need this one quick!

15254 days 15 hours ago


Offer By: Diana Matthews
for: 194.99

Offer By: John Dickinson
for: 185.99

为什么它只能获得FIRST记录并停止?

1 个答案:

答案 0 :(得分:0)

SELECT 
  listTitle, listLength, listCmt, listDt
  , GROUP_CONCAT(mBFName) as BFNames
  , GROUP_CONCAT(MBCLName) as MBCLNames
  , GROUP_CONCAT(mo_Amt) as Amounts
FROM User U
INNER JOIN Listing L on (U.uID = L.uID)
INNER JOIN MerchantOffer MO ON (L.listID = MO.listID)
INNER JOIN Merchant M on (M.mID = MO.mId) 
GROUP BY listTitle
ORDER BY listDt DESC;

请参阅:http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

请注意,group_concat默认使用','作为分隔符,但您也可以这样做:

GROUP_CONCAT(mo_Amt, SEPARATOR '<br/>') as Amounts  
-- or even
GROUP_CONCAT(CONCAT('<td>',moAmt,'</td>'), SEPARATOR '') as Amounts_in_a_table