使用循环仅显示每个 DISTINCT 字段值一次
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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在我看来,您想将
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 bylistTitle
.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 forlistTitle
and the others to havenull
- until the nextlistTitle
that's different.在每个字段都相同(listTitle、listLength、listCmt、listDt、mBCFName、mBCLName、moAmt、moDtOff)的基础上,SQL 中的更改最容易。
如果不相同,那么代码如何能够决定哪个 < 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
If it's not the same, then how would the code be able to decide which New Balance 574 Men's Shoes to display?
这并不那么容易 - 问题是为其他字段显示什么(例如“mBCFName”字段 - “Amanda”或“John”)?
使用“Group by”SQL 语句,然后定义规则(max、min、avg、GROUP_CONCAT ...)来选择其他行 - 示例:
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: