使用循环仅显示每个 DISTINCT 字段值一次

发布于 2024-12-08 12:42:44 字数 1538 浏览 1 评论 0原文

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;

这个 foreach() 循环

    $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>";
    }   

产生:

在此处输入图像描述

基本上我想要的是:

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>
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:

enter image description here

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青巷忧颜 2024-12-15 12:42:44

在我看来,您想将 listTitle 打印为相关评论上方的组标题

一种方法是跟踪前一行的 listTitle ,然后仅在与当前行存在差异时才打印它。当然,您必须确保结果集按 listTitle 排序。

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

也可能在查询中执行此操作,但这会很棘手,因为您希望具有该 listTitle 的第一条记录具有 listTitle 的值,而其他记录具有 listTitle 的值有 null - 直到下一个不同的 listTitle

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.

哀由 2024-12-15 12:42:44

在每个字段都相同(listTitle、listLength、listCmt、listDt、mBCFName、mBCLName、moAmt、moDtOff)的基础上,SQL 中的更改最容易。

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

如果不相同,那么代码如何能够决定哪个 < em>New Balance 574 男鞋要展示吗?

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?

昨迟人 2024-12-15 12:42:44

这并不那么容易 - 问题是为其他字段显示什么(例如“mBCFName”字段 - “Amanda”或“John”)?

使用“Group by”SQL 语句,然后定义规则(max、min、avg、GROUP_CONCAT ...)来选择其他行 - 示例:

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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文