2011-10-06 55 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