简体   繁体   English

以一对多的关系获取所有子记录

[英]Getting all sub records in a one-to-many relationship

I am running a query for my application, that is really making me wish I used ORM. 我正在运行我的应用程序的查询,这真的让我希望我使用ORM。 My table are structured as follows: 我的表格结构如下:

tabs 标签

  • id ID
  • name 名称
  • sort 分类

fields 领域

  • id ID
  • label 标签
  • tabid tabid

As you can assume there is a one-to-many relationship between fields and tabs. 您可以假设字段和制表符之间存在一对多关系。 What I would like to do is, using pure SQL if possible, create a query that has the tabs and underneath each tab shows a subquery of all fields. 我想做的是,如果可能的话,使用纯SQL创建一个包含选项卡的查询,每个选项卡下面都显示所有字段的子查询。

Currently I am just doing the following, but I was wondering if there is something better to do. 目前我正在做以下事情,但我想知道是否有更好的事情要做。

<cfquery name="local.tabQuery" attributeCollection="#Variables.dsn#">
     SELECT id,name FROM tabs ORDER BY sort
</cfquery>
<cfset local.tabs = [] />
<cfloop query="local.tabQuery">
     <cfquery name="local.fields" attributeCollection="#Variables.dsn3">
          SELECT * FROM fields WHERE tabid = <cfqueryparam value="#local.tabQuery.id#" cfsqltype="cf_sql_integer" />
     <cfquery>
     <cfset arrayAppend(local.tabs, local.fields) />
</cfloop>

Note: That is not my actual code, but that should, in theory, work just fine. 注意:这不是我的实际代码,但理论上应该可以正常工作。

You want grouped output. 你想要分组输出。

<cfquery name="local.tabQuery" attributeCollection="#Variables.dsn#">
    SELECT t.id, t.name, t.sort, f.id AS fieldID, f.label
    FROM tabs t  INNER JOIN fields f ON t.id = f.tabID
    ORDER BY t.sort
</cfquery>

<cfoutput query="local.tabQuery" group="sort">
    Tab: #local.tabQuery.name#<br>

    <cfoutput>
        Field: #local.tabQuery.label#<br>
    </cfoutput>
</cfoutput>

You definitely want the CFOUTPUT group, feature, but I think you might want a join in your query. 你肯定想要CFOUTPUT组,功能,但我想你可能想要在你的查询中加入。 Unless there's something I'm not seeing, running a secondary CFQUERY inside a looped CFOUTPUT or other loop sounds like a lot of work for the database server. 除非有一些我没有看到的东西,在循环CFOUTPUT或其他循环中运行辅助CFQUERY听起来像数据库服务器的很多工作。 What about 关于什么

SELECT T.id, T.name, F.id, F.label
FROM tabs T JOIN fields F ON F.tabid = T.id
ORDER BY T.sort, F.label

Then use the group CFOUTPUT clause based on the tab name field 然后根据选项卡名称字段使用组CFOUTPUT子句

<cfoutput query="local.tabQuery" group="name">
    Tab: #local.tabQuery.name#<br>

    <cfoutput>
        Field: #local.tabQuery.label#<br>
    </cfoutput>
</cfoutput>

If you always want to show all tabs, even if you don't have a field in it, make the JOIN a LEFT OUTER JOIN. 如果您总是想要显示所有选项卡,即使您没有字段,也请将JOIN设置为LEFT OUTER JOIN。 Here's an example using the cfartgallery database that should be built into your development server. 以下是使用cfartgallery数据库的示例,该数据库应该内置到您的开发服务器中。 It shows all the artists and any works they may have. 它显示了所有艺术家和他们可能拥有的任何作品。

<cfquery name="qryArtistsWithWorks" datasource="cfartgallery">
    SELECT A1.artistID, A1.lastname || ', ' || A1.firstname as artistName,
                A2.artID, A2.artName, A2.description
    FROM artists A1 LEFT OUTER JOIN art A2 ON A2.artistID = A1.artistID
    ORDER BY artistName, A2.artName
</cfquery>


<cfoutput query="qryArtistsWithWorks" group="artistId">

    <dl>
        <dt>#qryArtistsWithWorks.artistName#</dt>

        <cfoutput>
        <dd>#qryArtistsWithWorks.artName#</dd>
        </cfoutput>

    </dl>
</cfoutput>

Make sure the field you group on uniquely identifies the groups you want to build (ie no two records have the same value for 'sort'). 确保您分组的字段唯一标识您要构建的组(即没有两个记录具有相同的'sort'值)。 You should generally use something like a primary key or a label field as your grouping attribute. 您通常应该使用类似主键或标签字段的内容作为分组属性。 If you don't, CF may group things together into one group in ways that you are not expecting. 如果不这样做,CF可能会以您不期望的方式将事物分组到一个组中。

In the example above, two artists might have the same first and last name, but their art works would not be collapsed into a single list. 在上面的示例中,两位艺术家可能具有相同的名字和姓氏,但他们的艺术作品不会折叠成单个列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM