1

我对以下代码的结果进行排序时遇到问题。我有一个动态排序字段,满足某个值,它将按指定字段排序。但是,由于某些原因,我似乎无法按某些字段排序,结果似乎按一些未知的顺序排序......例如。i.totalExclTax - 如果我按此排序,则结果未排序。因此我想知道为什么它不能按该字段排序

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;
4

1 回答 1

0

只要 'B'='B' 总是输出第二个值(i.totalExclTax)作为结果。在 MySQL 中,尝试使用与您的语句等效的语句:

ORDER BY 
if('B'='A', i.invoiceNum,if('B'='B',i.totalExclTax,'Customer Name')) 
ASC;
于 2012-08-15T06:41:06.240 回答