创建动态html表,其中td值与ms sql db中的值相关联

时间:2013-04-08 16:54:18

标签: jquery sql-server coldfusion

使用ColdFusion 9服务器和MS-SQL数据库,我正在尝试创建html表,其中td值与同一MS-Sql数据库表中的值相关联。

查询1 - 获取部门:

<cfquery datasource="datasrc" name="qGetDep">
    SELECT DISTINCT Dept
    FROM someTable
    WHERE isnull(Dept, '') != ''
    ORDER BY DEPT ASC
</cfquery>

查询2 - 获取姓名

<cfquery datasource="datasrc" name="qGetNam">
    SELECT a.Dept, a.names
    FROM someTable as a
    INNER JOIN
    (
    SELECT DISTINCT Dept, MIN(ID) as ID, names
    FROM someTable
    GROUP BY Dept, names
    ) as b
    ON a.Dept = b.Dept
    AND a.ID = b.ID
    WHERE isnull(a.Dept, '') != ''
    ORDER BY a.Dept ASC
</cfquery>

index.cfm

<table border="1" id="table1">
    <thead>
        <tr>
            <cfoutput query="qGetDep">
                <th><b>#DEPT#</b></th>
            </cfoutput>
        </tr>
    </thead>
    <tbody>
        <cfoutput query="qGetNam">
            <tr>
                <cfloop query="qGetDep">
                    <cfset cls= qGetDep.Dept>
                    <cfif qGetNam.Dept EQ cls >
                        <td class="#cls#">#qGetNam.names#</td>
                    <cfelse>
                        <td class="#cls#"></td>
                    </cfif>

                </cfloop>
            </tr>
        </cfoutput>
    </tbody>
</table>

使用db中的当前数据,这将输出与此类似的表: enter image description here

我需要它是这样的。 enter image description here
jQuery解决方案是可以接受的 任何帮助将不胜感激 谢谢。

1 个答案:

答案 0 :(得分:0)

我是基于我对你的'实际私人数据'的假设来回答这个问题。

希望即使我的假设是错误的,这可能会让你朝着正确的方向前进。

如果这是你的 -

  

部门表

Department Table

这是你的

  

人员表

People Table

然后使用此查询 -

SELECT *
  FROM (
        SELECT a.id AS pid
              ,a.NAME
              ,b.dept
          FROM deptPeople a
              ,deptTable b
         WHERE a.dept = b.dept
       ) AS A
  PIVOT(
        MIN(NAME) FOR dept IN ( dept1, dept2, dept3, dept4, dept5)
       ) AS B

会给你

Final Result

将ColdFusion中的所有内容组合在一起 -

<cfquery datasource="que_tempdb_int" name="qGetDep">
    SELECT dept
    FROM deptTable
    ORDER BY DEPT ASC
</cfquery>

<cfset allDept = valuelist(qGetDep.dept,',') />

<cfquery name="qGetNam" datasource="que_tempdb_int">
    SELECT *
      FROM (
            SELECT a.id AS pid
                  ,a.NAME
                  ,b.dept
                  FROM deptPeople a
                  ,deptTable b
             WHERE a.dept = b.dept
           ) AS A
      PIVOT(
            MIN(NAME) FOR dept IN (#allDept#)
           ) AS B
</cfquery>

<table border="1" id="table1">
    <thead>
        <tr>
            <cfoutput query="qGetDep">
                <th><b>#DEPT#</b></th>
            </cfoutput>
        </tr>
    </thead>
    <tbody>
    <cfoutput query="qGetNam">
        <tr>
            <cfloop list="#allDept#" index="dept" delimiters=",">
            <td>#qGetNam[dept][qGetNam.currentRow]#</td>
            </cfloop>
        </tr>
    </cfoutput>
    </tbody>
</table>

最终结果 -

HTML Pivot Table