简体   繁体   English

使用GROUP_CONCAT的SQL查询产生意外结果

[英]Unexpected results from SQL Query using GROUP_CONCAT

I'm having an issue with an sql query and I'm not sure what I am doing wrong. 我在使用sql查询时遇到问题,但不确定自己在做什么错。 Anyways let me explain: 无论如何,让我解释一下:

Initially this was the original query: 最初,这是原始查询:

SELECT cl.*,
       c.id,c.type,
       c.firstname,
       c.surname,
       c.job,
       c.company,
       c.directorycompany_id,
       dc.id, dc.name, 
       es.id FROM contactlist_contact cl 
INNER JOIN contact c ON cl.contact_id = c.id 
LEFT JOIN directorycompany dc ON dc.id = c.directorycompany_id 
LEFT JOIN expertsection es ON es.id = c.expertsection_id 
WHERE cl.contactlist_id = 36311 
ORDER BY dc.surname

The statement fetches all of the details from the contactlist table where the id is X. The information it returns is a row for each contact in the contactlist table along with information on the company (directorycompany) they work for and various other details about the contact from the contact table. 该语句从id为X的contactlist表中获取所有详细信息。它返回的信息是contactlist表中每个联系人的一行,以及有关他们工作的公司(directorycompany)的信息以及有关该联系人的各种其他详细信息从联系表。 So the information looks something like this: 因此,信息如下所示:

contactlist_id  contact_id  id  active  id  type    firstname   surname     job       company   directorycompany_id id  name    id
36311   1939    316955375   1   1939    directory   Joe         Bloggs      Deputy Editor       786 786 Herald People   0 
36311   1935    316955374   1   1935    directory   Jim         Bloggs      Advertising Manager 786 786 Herald People   0
36311   28034   316955373   1   28034   directory   Jay         Bloggs      News Reporter       786 786 Herald People   0

I then went and attempted to modify the above SQL as additional functionality was required but I've been seeing unwanted results. 然后我去尝试修改上面的SQL因为需要附加功能,但是我一直看到不想要的结果。 Basically I am trying to JOIN 3 other tables 基本上我正在尝试JOIN其他3个表

  • directorycolumn 目录栏
  • directorysupplement 目录补充
  • directoryprogramme 目录程序

The idea being that it would return all of the columns, supplements and programmes that the contact in the contactlist has also written. 其想法是,它将返回联系人列表中的联系人也编写的所有列,补充和程序。 Also to point out, in some cases a contact may have written more than 1 column, supplement or programme and as a result I ideally wanted to display this in the same row as the contact as opposed to duplicating the rows so I used the GROUP_CONCAT() function. 另外要指出的是,在某些情况下,某个联系人可能编写了多于1列,补充或程序,因此,理想情况下,我希望将其显示在与该联系人相同的行中,而不是复制行,因此我使用了GROUP_CONCAT()功能。

This is the modified SQL 这是修改后的SQL

SELECT cl.*,
       c.id,
       c.type,
       c.firstname,
       c.surname,
       c.job,
       c.company,
       c.directorycompany_id, 
       dc.id, dc.name, 
       es.id, 
       GROUP_CONCAT(dirc.name) AS gcname,
       GROUP_CONCAT(dirp.name) AS gpname, 
       GROUP_CONCAT(dirs.name) AS gsname 
    FROM contactlist_contact cl 
    INNER JOIN contact c ON cl.contact_id = c.id 
    LEFT JOIN directorycompany dc ON dc.id = c.directorycompany_id 
    LEFT JOIN expertsection es ON es.id = c.expertsection_id 
    LEFT JOIN directorycolumn dirc ON dirc.directorycontact_id = c.id 
    LEFT JOIN directoryprogramme dirp ON dirp.directorycontact_id = c.id 
    LEFT JOIN directorysupplement dirs ON dirs.directorycontact_id = c.id
    WHERE cl.contactlist_id = 36311 
    ORDER BY dc.surname

This returns: 返回:

contactlist_id  contact_id  id  active  id  type    firstname   surname job company directorycompany_id id  name    id  gcname                     gpname      gsname
36311   28034   316955373   1   28034   directory   Jay           Bloggs    News Reporter       786 786 Herald People   0   The Arts Scene,Farming        \N         \N

So my question is, where have the other 2 results gone and why are they not showing? 所以我的问题是,其他2个结果哪里去了,为什么不显示呢? And also why is the information in gcname being displayed for this contact when in fact it is related to the contact with the id 1939 以及为什么实际上在gcname中的信息与ID为1939的联系人有关时显示此联系人的原因

if you remove GROUP_CONCAT it would display correct records, because when you use this function you should have GROUP BY clause. 如果删除GROUP_CONCAT,它将显示正确的记录,因为使用此函数时,您应该具有GROUP BY子句。 Currently it will consider all records as a single group. 当前,它将所有记录视为一个组。

If you look values in gcname is multiple, which is correct. 如果您在gcname中查找的值是多个,那是正确的。

Group_concat is part of mysql aggregate functions . Group_concat是mysql聚合函数的一部分。 That means it will group all equal values together into one row, in your case all three columns have the same value, thats why you only get one as result. 这意味着它将所有相等的值组合到一行中,在您的情况下,所有三列都具有相同的值,这就是为什么只得到一个结果的原因。 what result would you expect using group_concat? 您希望使用group_concat有什么结果?

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

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