Display each DISTINCT Field Value only once using loop

落爺英雄遲暮 提交于 2019-12-31 05:23:20

问题


SELECT listTitle, listLength, listCmt, listDt, mBCFName, mBCLName, moAmt, moDtOff
FROM User U, Listing L, Merchant M, MerchantOffer MO
WHERE U.uID = L.uID
and L.listID = MO.listID
and M.mID = MO.mId
ORDER BY listDt DESC;

This foreach() loop

    $result = $sth->fetchAll(PDO::FETCH_ASSOC);
    foreach($result as $row) 
    { 
    echo "<div class='listing'>";
        print '<br>Title: ' . $row['listTitle'] . '<br>Comment: ' . $row['listCmt'] . 
        '<br>Date: ' . $row['listDt'] . '<br>Offer By: ' . $row['mBCFName']. ' ' .$row['mBCLName']. '<br> for: ' . $row['moAmt'];
    echo "</div>";
    }   

produces:

Basically what I want is:

Title: Apple iPhone 4S (listTitle)
Days: <some day amount <listLength>
Comment: some comment <listCmt>

    Offer By: some user <mBCFName mBCLName>
    Offer:  19.99 <moAmt>
    Date: 10/03/2011 < moDtOff>

    Offer By: some user <mBCFName mBCLName>
    Offer:  19.99 <moAmt>
    Date: 10/03/2011 < moDtOff>

    Offer By: some user <mBCFName mBCLName>
    Offer:  19.99 <moAmt>
    Date: 10/03/2011 < moDtOff>

    Offer By: some user <mBCFName mBCLName>
    Offer:  19.99 <moAmt>
    Date: 10/03/2011 < moDtOff>

回答1:


It sounds to me like you want to print listTitle as a group heading above the relevant comments.

One way to do it would be to keep track of listTitle of the previous row, and then only print it if there's a difference with the current row. Of course, you'd have to make sure your result set is ordered by listTitle.

Another way would be to have one query that gets all data for that group heading, then another query that gets the contents of the group.

It is also probably possible to do it in the query, but that will be tricky since you want the first record with that listTitle to have a value for listTitle and the others to have null - until the next listTitle that's different.




回答2:


On the basis that every field is the same (listTitle, listLength, listCmt, listDt, mBCFName, mBCLName, moAmt, moDtOff) the change is easiest in the SQL

SELECT DISTINCT listTitle, listLength, listCmt, listDt, mBCFName, mBCLName, moAmt, moDtOff

If it's not the same, then how would the code be able to decide which New Balance 574 Men's Shoes to display?




回答3:


This is not that easy - the question is what to show for the other field (for example the "mBCFName" field - "Amanda" OR "John")?

use the "Group by" SQL statement and then define the rules (max,min,avg, GROUP_CONCAT ...) to select the other rows - Example:

SELECT listTitle, min(listLength), min(listCmt), min(listDt), GROUP_CONCAT(mBCFName), min(mBCLName), min(moAmt), min(moDtOff)
FROM User U, Listing L, Merchant M, MerchantOffer MO
WHERE U.uID = L.uID
  and L.listID = MO.listID
  and M.mID = MO.mId
GROUP BY listTitle
ORDER BY listDt DESC;


来源:https://stackoverflow.com/questions/7668115/display-each-distinct-field-value-only-once-using-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!