2011-10-06 30 views
0

更新的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 

它爲什麼只得到第一個記錄和停止?

回答

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 
+0

更多的輸出問題。你能看到我包括的上述*新*圖片嗎?它不顯示每個人的名字,如199.99,請參閱上面的內容! – Jay

+0

@Jay,我不是在這裏以複製粘貼的形式爲你寫代碼,我只是指出'哦這是一種處理代碼的方法。 – Johan

+0

我很欣賞Johan。請? – Jay

相關問題