我正在学习ColdFusion。我想显示一篇文章和3张图片。我的代码可以编译,但是显示了3次和3张图片。我只想显示一篇文章。有人可以看看我的代码并给我提示吗?谢谢你的时间!
<html>
<head>
<title>Hello</title>
</head>
<body>
<h3>Full Article View</h3>
<CFQUERY NAME="myQuery1" Datasource="mydb" >
SELECT *
FROM articles
INNER JOIN article_image_mapping ON articles.article_ID = article_image_mapping.aim_articleID
INNER JOIN images ON aim_imageID = images.image_ID
WHERE articles.article_ID = #URL.ID#
</CFQUERY>
<div align="left">
<cfoutput query="myQuery1">
#ucase(myquery1.article_title)# <br/>
-------------------------------------<br>
#myquery1.article_author# :: #myquery1.article_date#<br/>
#myquery1.article_content#<br/>
#myquery1.image_thumbpath#<br/>
</cfoutput>
</div>
</body>
</html>
最佳答案
您需要在查询中使用group
属性,并在查询中添加ORDER BY
。我还建议仅列出SELECT语句中所需的列
<CFQUERY NAME="myQuery1" Datasource="mydb" >
SELECT article_title, article_author, article_content, image_thumpath
FROM articles
INNER JOIN article_image_mapping ON articles.article_ID = article_image_mapping.aim_articleID
INNER JOIN images ON aim_imageID = images.image_ID
WHERE articles.article_ID = <cfqueryparam cfsqltype="cf_sql_integer" value="#URL.ID#">
ORDER BY article_title <!--- this is important! --->
</CFQUERY>
<div align="left">
<cfoutput query="myQuery1" group="article_title"><!--- show the article_title once --->
#ucase(myquery1.article_title)# <br/>
-------------------------------------<br>
<cfoutput><!--- loop through all results with same article title --->
#myquery1.article_author# :: #myquery1.article_date#<br/>
#myquery1.article_content#<br/>
#myquery1.image_thumbpath#<br/>
</cfoutput>
</cfoutput>
</div>
关于mysql - 如何删除重复的内容?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22900311/