Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 具有多对多关系_Mysql_Coldfusion_Cfquery_Cfoutput - Fatal编程技术网

Mysql 具有多对多关系

Mysql 具有多对多关系,mysql,coldfusion,cfquery,cfoutput,Mysql,Coldfusion,Cfquery,Cfoutput,我正在为我们的会计部门做一个项目。我有一个数据库MySQL表和分类账代码。我们公司有几个不同的办公地点,每个代码都可以应用于一个或多个办公地点。每个办公地点都可以有一个或多个适用的分类账代码。因此,我与一个包含代码id和位置id的桥接表存在多对多关系。我的SQL如下所示: SELECT gl.`code_id`, gl.`account_code`, gl.`account_type`, gl.`account_desc`, glloc.`location_id` FROM `gl_codes`

我正在为我们的会计部门做一个项目。我有一个数据库MySQL表和分类账代码。我们公司有几个不同的办公地点,每个代码都可以应用于一个或多个办公地点。每个办公地点都可以有一个或多个适用的分类账代码。因此,我与一个包含代码id和位置id的桥接表存在多对多关系。我的SQL如下所示:

SELECT gl.`code_id`, gl.`account_code`, gl.`account_type`, gl.`account_desc`, glloc.`location_id`
FROM `gl_codes` as gl
    LEFT JOIN `gl_codes_locations` as glloc
ON gl.`code_id` = glloc.`code_id`
ORDER BY gl.`code_id`, glloc.`location_id`
这将生成一个表,每个代码id/位置id对有一个单独的行。我想使用cfoutput在表中显示它。我只希望每个代码\u id对应一行,但我将在每行中使用一列来标记该代码是否应用于给定的位置\u id,如下所示:

|CodeAccount | CodeType | CodeDescription |代码位置| | | | | 1 | 2 | 3 | 4 | |一些账户|一些代码|一些描述| X | X ||


我知道我不能用多个查询属性嵌套cfoutput标记。我试过一些分组,但似乎做不好。请帮忙 这会让你离得很近。首先,我们需要一个可用ID的列表,这样我们就知道需要多少Location子列

按位置id从总帐代码位置订单中选择位置id 然后,在表中,我们可以使用以下信息构建标题和正文:

代码ID 代码帐户 代码类型 代码描述 代码位置 HtmlEditFormatid HtmlEditFormatcode\u id HtmlEditFormataccount_代码 HtmlEditFormataccount\u类型 HtmlEditFormataccount\u desc ListFindCurrLocationId,id gt 0?“X':
多亏了@Tomalac和他的价值清单建议,我才能够将其应用到我的代码中,并让它按照我想要的方式工作。分栏提示很好,我可能在将来实现它,但现在我们处理的是固定数量的位置

有关完整的代码如下,以供参考。出于隐私原因,我编辑了位置名称

<table class="table table-striped table-bordered">
    <thead class="bg-nav text-white">
        <tr>
            <th scope="col" rowspan="2" class="align-middle">Code</th>
            <th scope="col" rowspan="2" class="align-middle">Type</th>
            <th scope="col" rowspan="2" class="align-middle">Description</th>
            <th scope="col" colspan="4" class="text-center">Applies To</th>
            <th scope="col" rowspan="2" class="text-center align-middle">Edit</th>
            <th scope="col" rowspan="2" class="text-center align-middle">Delete</th>
        </tr>
        <tr>
            <th scope="col">Chicago</th>
            <th scope="col">Detroit</th>
            <th scope="col">LA</th>
            <th scope="col">New York</th>
        </tr>
    </thead>
    <tbody>
        <cfoutput query="codes" group="code_id">
                <tr>
                    <!--- Use function in cfcomponent to grab the location(s) that pertain to the given code_id --->
                    <!--- Dump query results into ValueList --->
<cfset codeLocations = ValueList(createObject("component", "com.modules.glcodes").getCodeLocations("query", codes.code_id).location_id)>
                    <td>#account_code#</td>
                    <td>#account_type#</td>
                    <td>#account_desc#</td>
                    <td><cfif ListLen(codeLocations) GT 0 AND (ListContains(codeLocations, "3") GT 0)>X</cfif></td>
                    <td><cfif ListLen(codeLocations) GT 0 AND (ListContains(codeLocations, "2") GT 0)>X</cfif></td>
                    <td><cfif ListLen(codeLocations) GT 0 AND (ListContains(codeLocations, "4") GT 0)>X</cfif></td>
                    <td><cfif ListLen(codeLocations) GT 0 AND (ListContains(codeLocations, "1") GT 0)>X</cfif></td>
                    <td>Edit</td>
                    <td>Delete</td>
            </tr>
        </cfoutput>
    </tbody>
</table>

请编辑您的问题,以显示您在分组时尝试了什么以及尝试分组时发生了什么。如果可以避免,请不要对每个表行点击数据库服务器一次。创建一个包含整个表所有数据的查询。@Tomalak这是我没有想到的一个非常好的观点。最好先执行一个主查询,然后再执行一个查询查询来创建ValueList吗?任何避免命中数据库的操作都会提高页面性能。您已经按照代码id对代码查询进行了分组,因此使用像我这样的内部代码,您应该可以获得所需的id。除非分组,否则不能嵌套。分组后,组中的每个项目内部运行一次,而外部运行一次。如果您知道的话,它的工作原理与Crystal Reports中的组/组标题/组详细信息/组页脚机制类似。您可以通过这种方式设置多级分组,但始终需要对组字段进行预排序,即当组字段的值更改时,ColdFusion将启动一个新组。此外,请使用ListFind/ListFindLocase-而不是ListContains。后者搜索子字符串,而不是整个值。