开发者

Output the results of these two tables

UPDATED 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

Slightly updated 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);   
    ?>  

Gives output like: http://i.stack.imgur.com/pNfU5.png

It should really be outputting ... for example:

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

Why does it ONLY get the FIRST record and stop?


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;

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

Note that group_concat uses ',' as a separator by default, but you can also do:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜