我在排序以下代码的结果时遇到问题。 我有一个动态排序字段,通过该字段满足某个值,它将按指定的字段排序。 但是由于某些原因,我似乎无法按某些字段排序,结果似乎按某种未知顺序排序...... 例如。 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;
答案 0 :(得分:0)
一旦“B”=“B”,您的情况总是输出第二个值(i.totalExclTax)作为结果。在MySQL中尝试使用这个等效的语句:
ORDER BY
if('B'='A', i.invoiceNum,if('B'='B',i.totalExclTax,'Customer Name'))
ASC;