如何删除重复内容?

时间:2022-11-28 09:22:20

I'm learning ColdFusion. I would like to display an article and 3 pictures. My code compiles, but it shows the article 3 times and 3 pictures. I just want to show the article one time. Can someone take a look at my code and give me a hint? Thanks for your time!

我正在学习ColdFusion。我想展示一篇文章和三张照片。我的代码是编译的,但是它显示了文章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>

2 个解决方案

#1


2  

You need to use the group attribute on your query and also put an ORDER BY in your query. I'd also suggest only listing out the columns you need in your SELECT statement

您需要在查询中使用group属性,并在查询中添加一个订单。我还建议只列出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>

#2


0  

Matt's right, you're looking for the group attribute in cfoutput. Although I think the placement of the inner cfoutput loop isn't quite what you're looking for. I think this is what you're trying to accomplish.

Matt说的对,你在寻找cfoutput中的group属性。虽然我认为内部cfoutput循环的位置不是您想要的。我想这就是你想要达到的目标。

<!--- use the group attribute to show the article once --->
<cfoutput query="myQuery1" group="article_id">
    #ucase(myquery1.article_title)# <br/>
    -------------------------------------<br>
    #myquery1.article_author# :: #myquery1.article_date#<br/>
    #myquery1.article_content#<br/>
    <!--- use an inner cfoutput to loop over the associated images --->
    <cfoutput>
        #myquery1.image_thumbpath#<br/>
    </cfoutput>
</cfoutput>

Anything that's inside your inner cfoutput is going to get one display for each record in your query. Items in the outer cfoutput (with the group attribute) will only appear once per group, so if your group is article_id, it will only show once for a given article_id.

在您的内部cfoutput内的任何内容都将为查询中的每个记录显示一个显示。外部cfoutput(带有组属性)的项只会在每个组中出现一次,所以如果您的组是article_id,那么它只会显示一个给定的article_id。

In this particular case, you should only have one record in the articles table, so you don't need to worry about the sort order, since the images will all be attached to that record and that should cause them to occur next to each other in the result query*. If you were going to group on some other column however, then Matt would be correct that you would need to make sure your records are sorted correctly -- if the grouped records aren't next to each other in the query, then they won't be grouped in the output.

在这个特殊的例子中,您应该只在articles表中有一个记录,所以您不需要担心排序顺序,因为这些图像都将被附加到该记录,这将导致它们在结果查询*中彼此相邻。但是,如果您打算在其他一些列上进行分组,那么Matt将是正确的,您需要确保您的记录被正确地排序——如果在查询中,分组记录不是相邻的,那么它们将不会被分组到输出中。

Unrelated: I would also recommend cfqueryparam for the article_id in your cfquery above, because numeric columns are particularly susceptible to SQL injection attacks.

不相关的:我还建议在上面的cfquery中为article_id提供cfqueryparam,因为数字列特别容易受到SQL注入攻击的影响。

(* as long as you're not doing something odd like sorting by the image_thumbpath column -- although technically the standard for SQL says you're not guaranteed any kind of sorting unless you explicitly specify it in an order by clause)

(*只要你不做类似于image_thumbpath列排序的事情,尽管严格来说,SQL的标准是,你不能保证任何排序,除非你在order by子句中显式地指定它)

#1


2  

You need to use the group attribute on your query and also put an ORDER BY in your query. I'd also suggest only listing out the columns you need in your SELECT statement

您需要在查询中使用group属性,并在查询中添加一个订单。我还建议只列出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>

#2


0  

Matt's right, you're looking for the group attribute in cfoutput. Although I think the placement of the inner cfoutput loop isn't quite what you're looking for. I think this is what you're trying to accomplish.

Matt说的对,你在寻找cfoutput中的group属性。虽然我认为内部cfoutput循环的位置不是您想要的。我想这就是你想要达到的目标。

<!--- use the group attribute to show the article once --->
<cfoutput query="myQuery1" group="article_id">
    #ucase(myquery1.article_title)# <br/>
    -------------------------------------<br>
    #myquery1.article_author# :: #myquery1.article_date#<br/>
    #myquery1.article_content#<br/>
    <!--- use an inner cfoutput to loop over the associated images --->
    <cfoutput>
        #myquery1.image_thumbpath#<br/>
    </cfoutput>
</cfoutput>

Anything that's inside your inner cfoutput is going to get one display for each record in your query. Items in the outer cfoutput (with the group attribute) will only appear once per group, so if your group is article_id, it will only show once for a given article_id.

在您的内部cfoutput内的任何内容都将为查询中的每个记录显示一个显示。外部cfoutput(带有组属性)的项只会在每个组中出现一次,所以如果您的组是article_id,那么它只会显示一个给定的article_id。

In this particular case, you should only have one record in the articles table, so you don't need to worry about the sort order, since the images will all be attached to that record and that should cause them to occur next to each other in the result query*. If you were going to group on some other column however, then Matt would be correct that you would need to make sure your records are sorted correctly -- if the grouped records aren't next to each other in the query, then they won't be grouped in the output.

在这个特殊的例子中,您应该只在articles表中有一个记录,所以您不需要担心排序顺序,因为这些图像都将被附加到该记录,这将导致它们在结果查询*中彼此相邻。但是,如果您打算在其他一些列上进行分组,那么Matt将是正确的,您需要确保您的记录被正确地排序——如果在查询中,分组记录不是相邻的,那么它们将不会被分组到输出中。

Unrelated: I would also recommend cfqueryparam for the article_id in your cfquery above, because numeric columns are particularly susceptible to SQL injection attacks.

不相关的:我还建议在上面的cfquery中为article_id提供cfqueryparam,因为数字列特别容易受到SQL注入攻击的影响。

(* as long as you're not doing something odd like sorting by the image_thumbpath column -- although technically the standard for SQL says you're not guaranteed any kind of sorting unless you explicitly specify it in an order by clause)

(*只要你不做类似于image_thumbpath列排序的事情,尽管严格来说,SQL的标准是,你不能保证任何排序,除非你在order by子句中显式地指定它)