MySQL查询HTML表使用过滤器,COUNT,GROUP BY / LIMIT / ORDER BY和子查询

时间:2017-04-11 10:34:25

标签: php html mysql sql

我有一个问题让我头晕了大约2天,我需要一些帮助。 Pleeeaaze!

我需要做的是从下面描述的表中获取数据,输出必须使用客户上的过滤器(名称,代码X / Y / Z),应用程序(名称,波形),migrationLabel显示每个应用程序的每条消息。 label,migrationMessage。(日期)使用分页(每页50页)。

每个应用程序都可以显示0到n个消息,我必须使用属性“rowspan”创建HTML表。

以下是表格:

“#:PK”

“@:FK”

  • 申请(#id,label,@ customerId)
  • customer(#id,name,codeX,codeY,codeZ)
  • erpApplication(#id,label,@ customerId)
  • migrationMessage(@applicationId,creationTime,closureDate,previsionalDate,@ labelId,@ authorId)
  • migrationLabel(#id,label,ordering)
  • 工作(@employeeId,@ applicationId,@roleId)
  • 员工(#id,用户名)

这是当前的查询:

<?php
$query = 'SELECT 
        c.id AS customerId, c.name AS customerName, c.X3Code, c.mfgProCode, c.as400Code, c.jdeCode,
        a.id AS appId, a.label AS appName, a.wave,
        mm.closureDate, mm.previsionalDate, mm.content,
        ml.id, ml.label,
        q.msgCount
        FROM (
            SELECT a.id
            FROM `application` a
            JOIN `customer` c ON c.id = a.customerId
            ORDER BY c.name
        ) l
        JOIN application a ON a.id = l.id
        JOIN `customer` c ON c.id = a.customerId
        LEFT JOIN (
            SELECT applicationId, COUNT(*) AS msgCount
            FROM application a
            JOIN migrationMessage mm ON mm.applicationId = a.id
            JOIN migrationLabel ml ON ml.id = mm.labelId
            JOIN customer c ON c.id = a.customerId
            ' . $where . '
            GROUP BY applicationId
            LIMIT ' . $limit . ' OFFSET ' . $offset . '
        ) q ON q.applicationId = a.id
        LEFT JOIN `migrationMessage`mm ON mm.applicationId = q.applicationId
        LEFT JOIN `migrationLabel` ml ON ml.id = mm.labelId
        JOIN `works` w ON w.applicationId = a.id '
        . ($user->isOperator() ? ' AND w.roleId = ' . Role::Rri : ' AND w.employeeId = ' . $user->id)
    . ' JOIN employee e ON e.id = w.employeeId'
        . ($user->isJustUser() ? ' AND e.siteId = ' . $user->siteId : '')
        . $where
        . ' ORDER BY c.name, a.label, ml.ordering'
        . 'LIMIT ' . $limit . ' OFFSET ' . $offset;

几乎所有东西都可以使用这个查询,当我得到一个带有几个消息的应用程序时会出现问题... 例如,“rowspan”(查询中的msgCount)为5,但最后3个消息与前2个页面不同,然后HTML显示完全错误。 See HTML result

1 个答案:

答案 0 :(得分:0)

我从头开始编写查询,并得出完全符合我需求的结果。

对于那些想知道的人,关键是RIGHT JOIN'ed查询:

<?php
$query = 'SELECT
        c.id AS customerId, c.name AS customerName, c.X3Code, c.mfgProCode, c.as400Code, c.jdeCode,
        a.id AS appId, a.label AS appName, a.wave,
        mm.closureDate, mm.previsionalDate, mm.content,
        ml.id, ml.label,
        q0.msgCount
        FROM application a
        JOIN customer c ON c.id = a.customerId
        LEFT JOIN migrationMessage mm ON mm.applicationId = a.id
        LEFT JOIN migrationLabel ml ON ml.id = mm.labelId
        JOIN `works` w ON w.applicationId = a.id '
        . ($user->isOperator() ? ' AND w.roleId = ' . Role::Rri : ' AND w.employeeId = ' . $user->id)
    . ' JOIN employee e ON e.id = w.employeeId'
        . ($user->isJustUser() ? ' AND e.siteId = ' . $user->siteId : '')

    . ' LEFT JOIN (
        SELECT applicationId, COUNT(*) AS msgCount
        FROM application a
        JOIN migrationMessage mm ON mm.applicationId = a.id
        JOIN migrationLabel ml ON ml.id = mm.labelId
        JOIN customer c ON c.id = a.customerId
        GROUP BY applicationId
    ) q0 ON q0.applicationId = a.id
    RIGHT JOIN (
        SELECT a.id
        FROM application a
        JOIN customer c ON c.id = a.customerId
        LEFT JOIN migrationMessage mm ON mm.applicationId = a.id
        LEFT JOIN migrationLabel ml ON ml.id = mm.labelId
        ' . $where . '
        GROUP BY a.id
        ORDER BY c.name, a.label, ml.ordering
        LIMIT ' . $limit . ' OFFSET ' . $offset . '
    ) q1 ON q1.id = a.id
    ORDER BY c.name, a.label, ml.ordering'