简体   繁体   English

MYSQL GROUP_CONCAT 与 ORDER BY 奇怪的排序结果

[英]MYSQL GROUP_CONCAT with ORDER BY strange sorting results

This is my working query, no errors, but the sorting by certain columns is strange.这是我的工作查询,没有错误,但按某些列排序很奇怪。

$query = mysql_query("SELECT ServiceType, Location, Destination, Name, PAX,
                      TransferTime, Ticket SUM(PAX) AS TotalPAX,
                      GROUP_CONCAT( Coupon SEPARATOR '<BR>') AS PasTicket,
                      GROUP_CONCAT( TransferTime SEPARATOR '<BR>') AS PasTransferTime,
                      GROUP_CONCAT( Location SEPARATOR '<BR>') AS PasLocation,
                      GROUP_CONCAT( Destination SEPARATOR '<BR>') AS PasDestination,
                      GROUP_CONCAT( Name SEPARATOR '<BR>') AS PasName,
                      GROUP_CONCAT( PAX SEPARATOR '<BR>') AS PasPAX
                      FROM Reservations WHERE Date = '$DateToday'
                      GROUP BY Ticket
                      ORDER BY ServiceType, TransferTime ASC");

This gives me the following results which are the correct data but in wrong order:这给了我以下结果,这些结果是正确的数据,但顺序错误:

REF | SERVICE   | TIME     | PAX | HOTEL      | FROM    | NAME            | TICKET
----+-----------+----------+-----+------------+---------+-----------------+----------------
17  | DEPARTURE | 15:00:00 | 1.0 | IBEROSTAR  | AIRPORT | FRANCIS EUGEN   | TICKET: #110588
18  | DEPARTURE | 15:20:00 | 2.0 | PARADISUS  | AIRPORT | MARTIN CIBBAR   | TICKET: #110589
19  | DEPARTURE | 16:40:00 | 2.0 | PARADISUS  | AIRPORT | JOHN CIA        | TICKET: #3376
20  | DEPARTURE | 16:00:00 | 2.0 | RIU PALACE | AIRPORT | ALAN KRAMER     | TICKET: #110589
21  | DEPARTURE | 16:10:00 | 2.0 | RIU PALACE | AIRPORT | JOHN WILLS      | TICKET: #110589
22  | DEPARTURE | 16:20:00 | 2.0 | RIU PALACE | AIRPORT | PETER LONG      | TICKET: #110589

When I GROUP BY Ticket the query works but the Times are out of order, however if I add ORDER BY Time ASC the ORDER BY does not work - GROUP BY Time does work but then I lose the GROUP BY Ticket but I cannot use GROUP BY Time anyways... Number 18, 20, 21 and 22 should be GROUPED together with Time in ORDER ASC.当我GROUP BY Ticket查询有效但时间有问题时,但是如果我添加ORDER BY Time ASC则 ORDER BY 不起作用 - GROUP BY Time有效但随后我丢失了GROUP BY Ticket但我不能使用GROUP BY Time无论如何, GROUP BY Time ...数字 18、20、21 和 22 应该与时间一起按顺序分组。

The ideal would be something like: WHERE Date = '$DateToday' GROUP BY Ticket, TransferTime (LIKE '%TransferTime%') ORDER BY ServiceType, TransferTime ASC .理想的情况是: WHERE Date = '$DateToday' GROUP BY Ticket, TransferTime (LIKE '%TransferTime%') ORDER BY ServiceType, TransferTime ASC This is because TransferTimes can differ by some minutes and GROUP BY is too strict.这是因为 TransferTimes 可能会有几分钟的差异,而 GROUP BY 过于严格。

Below is an example of what it should be, GROUPED BY Ticket Number and sorted by TIME ASC.下面是它应该是什么的示例,按票号分组并按时间 ASC 排序。 I have left the REF numbers as they were to make it clearer.我保留了 REF 编号,因为它们是为了更清楚。 Strange thing is, we have this working live but in MSSQL but the system is from 2013 and we need to pass it over to MYSQL and PHP.奇怪的是,我们有这个工作,但在 MSSQL 中,但系统是 2013 年的,我们需要将它传递给 MYSQL 和 PHP。

REF | SERVICE   | TIME     | PAX | HOTEL      | FROM    | NAME            | TICKET
----+-----------+----------+-----+------------+---------+-----------------+----------------
17  | DEPARTURE | 15:00:00 | 1.0 | IBEROSTAR  | AIRPORT | FRANCIS EUGEN   | TICKET: #110588
20  | DEPARTURE | 16:00:00 | 2.0 | RIU PALACE | AIRPORT | ALAN KRAMER     | TICKET: #110589
21  | DEPARTURE | 16:10:00 | 2.0 | RIU PALACE | AIRPORT | JOHN WILLS      | TICKET: #110589
22  | DEPARTURE | 16:20:00 | 2.0 | RIU PALACE | AIRPORT | PETER LONG      | TICKET: #110589
18  | DEPARTURE | 16:40:00 | 2.0 | PARADISUS  | AIRPORT | MARTIN CIBBAR   | TICKET: #110589
19  | DEPARTURE | 16:40:00 | 2.0 | PARADISUS  | AIRPORT | JOHN CIA        | TICKET: #3376

GROUP_CONCAT appears to be working fine. GROUP_CONCAT 似乎工作正常。 Any ideas whats going on?任何想法是怎么回事?

Screen dump image (names and flights changed).屏幕转储图像(名称和航班已更改)。 The data is complete and correct, but the sorting order of the ticket number is off.数据完整正确,但票号排序不正确。 I realize this is because I am using GROUP BY Time which is too strict because Time can vary somewhat.我意识到这是因为我使用的 GROUP BY Time 过于严格,因为时间可能会有所不同。

PS: The GROUP_BY SEPARATOR is a line break BR which for some reason is not being published. PS: GROUP_BY SEPARATOR 是一个换行符 BR,由于某种原因没有被发布。 屏幕转储

Order of GROUP_CONCAT result string GROUP_CONCAT 结果字符串的顺序

You simply haven't stated an order for the group_concats, which would be您只是没有说明 group_concats 的订单,这将是

GROUP_CONCAT(... ORDER BY ...)

eg例如

GROUP_CONCAT( Coupon ORDER BY Id SEPARATOR '<BR>') AS PasTicket

so the order of your group_concats is left to chance.所以你的 group_concats 的顺序是随机的。

Order of the rows行的顺序

As to the rows, you have stated an order, but you order your result by columns that are not in your select list, so this is kind of an invisible sort.至于行,您已经说明了一个顺序,但是您按不在选择列表中的列对结果进行排序,因此这是一种不可见的排序。 (An example for clarification: if I show a list of first and last names, but behind the scenes I order these persons by the city they live in, the names will seem unordered.) (一个澄清的例子:如果我显示一个名字和姓氏的列表,但在幕后我按照他们居住的城市对这些人进行排序,这些名字似乎是无序的。)

As you group by Ticket it is also important to know whether there is exactly one ServiceType and one TransferTime per Ticket .当您按Ticket分组时,了解每个Ticket是否只有一个ServiceType和一个TransferTime也很重要。 If not, you'd get the values arbitrarily picked, which is probably undesired.如果没有,您将获得任意选择的值,这可能是不受欢迎的。 (Illustration: If a ticket has rows with service types A and M and Z, one of these will be picked and it is left to chance which the DBMS picks.) (示例:如果工单具有服务类型为 A、M 和 Z 的行,则将选择其中之一,然后由 DBMS 自行选择。)

At last:最后:

And as has been mentioned, when you aggregate ( GROUP BY ), but SELECT * you will get arbitrarily picked values.正如已经提到的,当您聚合( GROUP BY )但SELECT *您将获得任意选择的值。 Say a ticket has two rows, one with DEPARTURE |假设一张票有两行,其中一行是 DEPARTURE | 15:00:00 and one with ARRIVAL | 15:00:00 和 ARRIVAL 之一 | 18:00:00, you may even end up with DEPARTURE | 18:00:00,你甚至可能以DEPARTURE告终 | 18:00:00 for the ticket. 18:00:00 取票。

Please use ORDER BY clause inside the GROUP_CONCAT function.请在 GROUP_CONCAT 函数内使用 ORDER BY 子句。 Here is an example query:这是一个示例查询:

SELECT GROUP_CONCAT(DISTINCT(QUOTE(id)) ORDER BY dob ASC) as children FROM `users` WHERE id IN ("218","221","214","215").

It has 4 important things:它有4个重要的东西:

  1. GROUP_CONCAT GROUP_CONCAT
  2. DISTINCT清楚的
  3. Quotes around the values价值引述
  4. Adds ORDER BY clause inside the GROUP_CONCAT function在 GROUP_CONCAT 函数中添加 ORDER BY 子句

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

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