Вывести результаты этих двух таблиц - PullRequest
0 голосов
/ 06 октября 2011

ОБНОВЛЕННЫЙ 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

Почему он ТОЛЬКО получает ПЕРВУЮ запись и останавливается?

1 Ответ

0 голосов
/ 07 октября 2011
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
...