mysql 5.6:group_concat求助

时间:2016-01-24 05:10:34

标签: mysql

我有桌子:

    CREATE TABLE IF NOT EXISTS `buildingAccess` (
    `id` int(10) unsigned NOT NULL,
      `building` varchar(16) NOT NULL,
      `person` varchar(16) NOT NULL,
      `enteryDate` datetime NOT NULL,
      `exitDate` datetime DEFAULT NULL
    ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;

    INSERT INTO `buildingAccess` (`id`, `building`, `person`, `enteryDate`, `exitDate`) VALUES
    (1, 'Lot B-3', 'Alice Jones', '2015-11-10 05:29:14', '2015-11-10 15:18:42'),
    (3, 'Lot B-3', 'Alice Jones', '2015-11-11 07:11:27', '2015-11-11 12:43:34'),
    (7, 'Lot B-3', 'Alice Jones', '2015-12-10 07:11:27', '2015-12-11 12:43:34'),
    (2, 'Lot B-3', 'Bill Mayhew', '2015-11-10 10:29:14', '2015-11-10 12:18:42'),
    (4, 'Lot B-3', 'Bill Mayhew', '2015-11-12 09:10:27', '2015-11-13 02:43:34'),
    (8, 'Lot B-3', 'Bill Mayhew', '2015-11-12 09:10:27', '2015-11-13 02:43:34'),
    (5, 'Lot B-3', 'Charlotte Ahn', '2015-12-01 05:29:14', NULL),
    (6, 'Lot B-3', 'Dennis Lowe', '2015-12-10 10:29:14', '2015-12-10 12:18:42');

    CREATE TABLE IF NOT EXISTS `buildingNotes` (
      `building` varchar(16) NOT NULL,
      `observer` varchar(16) NOT NULL,
      `observationDate` datetime NOT NULL,
      `note` varchar(64) NOT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    INSERT INTO `buildingNotes` (`building`, `observer`, `observationDate`, `note`) VALUES
    ('Lot B-3', 'Alice Jones', '2015-11-10 05:32:12', 'burned out light on pole South-22'),
    ('Lot B-3', 'Alice Jones', '2015-11-10 05:39:12', 'burned out light on pole West-7'),
    ('Lot B-3', 'Alice Jones', '2015-11-10 05:42:12', 'overfull trash can near pole North-11'),
    ('Lot B-3', 'Charlotte Ahn', '2015-12-01 06:09:14', 'change drawr running low at gate 3');

    ALTER TABLE `buildingAccess`
     ADD PRIMARY KEY (`id`), ADD KEY `building` (`building`,`person`,`enteryDate`,`exitDate`);

    ALTER TABLE `buildingNotes`
     ADD KEY `building` (`building`,`observer`,`observationDate`,`note`);

    ALTER TABLE `buildingAccess`
    MODIFY `id` int(10) unsigned NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=9;

我的目标是一个查询,它返回buildingAccess表中所有记录的列表。每个字段都应有一个notes字段,该字段是GROUP_CONCATbuildingNotes.note条记录的所有buildingAccess.enteryDate条目,buildingAccess.exitDate日期/时间由 select BA.building, BA.person, BA.enteryDate, IFNULL(BA.exitDate, NOW()), IFNULL( GROUP_CONCAT( '<p>', BN.observationDate, ': ', BN.observer, ': ', BN.note, '</p>' ORDER BY BN.observationDate ASC SEPARATOR '' ), '' ) from buildingAccess BA LEFT JOIN buildingNotes BN ON BN.building = BA.building AND BN.observationDate BETWEEN BA.enteryDate AND BA.exitDate group by BN.building 和{{1}括起来}}

我尝试了一些事情,但我坚持:

+----------+---------------+---------------------+---------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| building | person        | enteryDate          | exitDate            | observations                                                                                                                                                                                                                     |
+----------+---------------+---------------------+---------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Lot B-3  | Charlotte Ahn | 2015-12-01 05:29:14 | 2016-01-23 23:04:04 |                                                                                                                                                                                                                                  |
| Lot B-3  | Alice Jones   | 2015-11-10 05:29:14 | 2015-11-10 15:18:42 | <p>2015-11-10 05:32:12: Alice Jones: burned out light on pole South-22</p><p>2015-11-10 05:39:12: Alice Jones: burned out light on pole West-7</p><p>2015-11-10 05:42:12: Alice Jones: overfull trash can near pole North-11</p> |
+----------+---------------+---------------------+---------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

返回:

buildingAccess

即使没有buildingNotes条记录,我也希望看到所有其他<form action="/NewCases/" method=post class="form-horizontal"> <h2>Add New Cases: </h2> <div class="input-group"> <span class="input-group-addon">@</span> <input type="text" id="casename" name="casename" class="form-control" placeholder="Enter Case Name:" required> </div> <div class="input-group"> <span class="input-group-addon">@</span> <input type="text" id="adminname" name="adminname" class="form-control" placeholder="Enter Case Name:" value="{{ current_user.username }}"> </div> <div class="input-group"> <span class="input-group-addon">@</span> <input type="file" id="imagefile" name="imagefile" class="form-control" placeholder="Enter Hard Disk File:" required> </div> <div class="input-group"> <span class="input-group-addon">@</span> <input type="file" id="memimagefile" name="memimagefile" class="form-control" placeholder="Enter Memory File:" required> </div> <div class="control-group"> <div class="controls"> <button type="submit" class="btn btn-success">Signup</button> </div> </div> </form> 条记录。

我假设我不是&#34;按&#34;分组正确的事情,但我还没有找到合适的组合。

指针?

1 个答案:

答案 0 :(得分:1)

我认为这是因为使用了来自GROUP BY子句中的外连接表的BN.building。请尝试以下方法:

SELECT
      BA.building
    , BA.person
    , BA.enteryDate
    , IFNULL(BA.exitDate, NOW())
    , IFNULL (
              GROUP_CONCAT (
                            '<p>',
                            BN.observationDate, ': ',
                            BN.observer, ': ', BN.note,
                            '</p>'
                            ORDER BY BN.observationDate ASC
                            SEPARATOR ''
                            )
                ,''
              )
FROM buildingAccess BA
      LEFT JOIN buildingNotes BN ON BN.building = BA.building
                  AND BN.observationDate BETWEEN BA.enteryDate AND BA.exitDate
GROUP BY
      BA.building
    , BA.person
    , BA.enteryDate
    , IFNULL(BA.exitDate, NOW())

这可能是行太多,也许您需要一些其他方式来处理(例如)只获取日期而不是完整的日期时间。但是,您应该通过caluse定期指定组中查询的所有非聚合列。见MySQL GROUP BY Extension