简体   繁体   English

MySQL不能按某些指定的字段排序?

[英]Mysql cannot sort by some specified field?

I have a problem with sorting the results for the below codes. 我在对以下代码的结果进行排序时遇到问题。 I have a dynamic sorting field whereby a certain value is met, it will sort by a specified field. 我有一个动态排序字段,可以满足某个特定值,它将按指定字段进行排序。 However I can't seem to sort by some of the fields for some reasons and the result seems to sort by some unknown order... eg. 但是由于某些原因,我似乎无法按某些字段进行排序,结果似乎按某些未知顺序进行排序...例如。 i.totalExclTax - if I sort by this, the result is not sorted. i.totalExclTax-如果按此排序,则结果不排序。 Hence I was wondering why it can't sort by that field 因此,我想知道为什么它不能按该字段排序

SELECT
i.invoicedDateTime AS `(@datetime)Date`,
i.invoiceNum AS `Invoice Num`,
c.displayName AS `Customer Name`,
m.name AS `Membership`,
FORMAT(i.totalExclTax, 2) AS `Total (Excl Tax)`,
FORMAT( SUM(it.amount), 2 ) AS `Tax`,
FORMAT(i.totalInclTax, 2) AS `Total (Incl Tax)`
FROM
(smSales__invoice i
JOIN smCustomer__customer c ON(i.customerId = c.id)
   AND c.ts_Deleted IS NULL
     JOIN smCustomer__membership m ON (c.membershipId = m.id)
     AND m.ts_Deleted IS NULL
)
  JOIN smSales__invoiceTax it ON(it.invoiceId = i.id)
  AND it.ts_Deleted IS NULL
WHERE i.invoicedDateTime BETWEEN '2012-07-01 00:00:00' AND DATE_ADD('2012-08-31 00:00:00', INTERVAL 1 DAY)
AND i.invoicedOutletId IN ('230614c3761512c8db000a38a346e217')
AND i.type = 'sales'
AND i.voidedDateTime IS NULL
AND i.ts_Deleted IS NULL
GROUP BY i.id
ORDER BY
  CASE 'B'
    WHEN 'A' THEN i.invoiceNum
    WHEN 'B' THEN i.totalExclTax
    ELSE `Customer Name`
  END
ASC;

Your case always output the second value (i.totalExclTax) as a results as soon as 'B'='B' always. 您的案例总是在总是'B'='B'时输出第二个值(i.totalExclTax)作为结果。 In MySQL try to use this equivalent of your statement: 在MySQL中,尝试使用以下等效语句:

ORDER BY 
if('B'='A', i.invoiceNum,if('B'='B',i.totalExclTax,'Customer Name')) 
ASC;

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

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