简体   繁体   English

GROUP_CONCAT奇怪的结果

[英]GROUP_CONCAT strange result

I try to use group_concat to create more quickly xml outpout. 我尝试使用group_concat来更快地创建xml outpout。 The record number is different between traditional query. 传统查询的记录号不同。 Indeed, when my query use group concat, I have less record. 实际上,当我的查询使用group concat时,我的记录较少。

    SELECT GROUP_CONCAT(
    CONCAT('\n<p>\n',  
        CONCAT('\n<id>',paIndex,'</id>\n'),
        CONCAT('<prInitiales>',prInitiales,'</prInitiales>\n'),
        CONCAT('<paNomPren>',paNomPrenom,'\n',ttT_Traitement_P,'</paNomPren>\n'),                                   
        CONCAT('<ttTStatutP>',ttTStatutP,' - ',DATE_FORMAT(ttDateStatut,'%d/%m/%Y'),'\n',ttUserImportant,'</ttTStatutP>\n'),
        CONCAT('<paDossier1>',paDossier1,'\n',paDossier2,'</paDossier1>\n'),
        CONCAT('<paNumTel1>',paNumTel1,'\n',paNumTel2,'</paNumTel1>\n'),
        CONCAT('<paNaissanceS>',DATE_FORMAT(paNaissance,'%d/%m/%Y'),'</paNaissanceS>\n'),
    '</p>') ORDER BY paNomPrenom DESC) AS xml
    FROM 20Patients_1012
    JOIN 30Traitemnt_201223 ON 20Patients_1012.paIndex = 30Traitemnt_201223.ttIndex
    JOIN 12Praticien_02 ON 30Traitemnt_201223.ttPraticien = 12Praticien_02.prIndex

The traditional query: 传统的查询:

    SELECT 20Patients_1012.paIndex, 20Patients_1012.paNomPrenom, 20Patients_1012.paDossier1, 20Patients_1012.paDossier2, 20Patients_1012.paNaissance, 20Patients_1012.paNumTel1, 30Traitemnt_201223.ttTStatutP, 30Traitemnt_201223.ttDateStatut, 12Praticien_02.prInitiales
FROM 20Patients_1012
JOIN 30Traitemnt_201223 ON 20Patients_1012.paIndex = 30Traitemnt_201223.ttIndex
JOIN 12Praticien_02 ON 30Traitemnt_201223.ttPraticien = 12Praticien_02.prIndex ORDER BY 20Patients_1012.paNomPrenom ASC

Thanks for helping 谢谢你的帮助

As stated in the manual : 手册所述

The result is truncated to the maximum length that is given by the group_concat_max_len system variable, which has a default value of 1024. The value can be set higher, although the effective maximum length of the return value is constrained by the value of max_allowed_packet . 结果被截断为group_concat_max_len系统变量给出的最大长度,该变量的默认值为1024.尽管返回值的有效最大长度受max_allowed_packet的值约束,但该值可以设置得更高。 The syntax to change the value of group_concat_max_len at runtime is as follows, where val is an unsigned integer: 在运行时更改group_concat_max_len的值的语法如下,其中val是无符号整数:

 SET [GLOBAL | SET [GLOBAL | SESSION] group_concat_max_len = val ; SESSION] group_concat_max_len = val ; 

You used LEFT JOIN in a query and simple JOIN in the other. 您在查询中使用了LEFT JOIN,在另一个中使用了简单的JOIN。 This may lead to different results because LEFT JOIN also consider records that do not have a match in the other table. 这可能会导致不同的结果,因为LEFT JOIN还会考虑在另一个表中没有匹配的记录。

I found the solution. 我找到了解决方案。 Indeed, if null value, record is ignored, so I use, COALESCE function to solve that! 的确,如果空值,记录被忽略,那么我用COALESCE函数来解决!

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

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