仅使用循环显示每个DISTINCT字段值一次 [英] Display each DISTINCT Field Value only once using loop

查看:86
本文介绍了仅使用循环显示每个DISTINCT字段值一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这个foreach()循环

  $ result = $ sth-> fetchAll(PDO :: FETCH_ASSOC); 
foreach($ result as $ row)
{
echo< div class ='listing'>;
print'< br>标题:'。 $ row ['listTitle']。 '< br>评论:'。 $ row ['listCmt']。
'< br>日期:'。 $ row ['listDt']。 '< br>提供者:'。 $行[ mBCFName]。 ''。$ row ['mBCLName']。 <峰; br>为:'。 $行[ moAmt];
回显< / div>;
}

产生:



基本上我想要的是:
$标题:Apple iPhone 4S(listTitle)
天数:< some day amount< listLength>
评论:一些评论< listCmt>

优惠方:某位使用者< mBCFName mBCLName>
优惠:19.99< moAmt>
日期:10/03/2011< moDtOff>

优惠方:某位使用者< mBCFName mBCLName>
优惠:19.99< moAmt>
日期:10/03/2011< moDtOff>

优惠方:某位使用者< mBCFName mBCLName>
优惠:19.99< moAmt>
日期:10/03/2011< moDtOff>

优惠方:某位使用者< mBCFName mBCLName>
优惠:19.99< moAmt>
日期:10/03/2011< moDtOff>


解决方案

这听起来像是要打印 listTitle 作为组标题 上方的相关评论。

跟踪前一行的 listTitle ,然后只在与当前行存在差异时才打印它。当然,你必须确定你的结果集是由 listTitle



命令的另一种方法是有一个查询获得该组标题的所有数据,然后另一个查询获取该组的内容。

在查询中也可以这样做,但这样做会很棘手,因为您希望第一条记录 listTitle 具有 listTitle 和其他值要有 null - 直到下一个 listTitle 不同。


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>

解决方案

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.

这篇关于仅使用循环显示每个DISTINCT字段值一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆