简体   繁体   English

MySQL排序3列

[英]MySQL sort 3 columns

I am trying to sort MySQL result from qry_ledger_all_balance and it's working fine, but the problem is I can only sort by PaymentDate . 我正在尝试从qry_ledger_all_balance sort MySQL结果,它工作正常,但问题是我只能按PaymentDate排序。

I want to sort in the order of: 我想按以下顺序排序:

  1. PaymentDate value PaymentDate
  2. Debit value Debit
  3. Credit value Credit

I am unable to do that. 我做不到。 I don't know what I am doing wrong. 我不知道我在做什么错。 Here is my Table Structure Sample. 这是我的表结构示例。

+-----------------+-------+--------+---------------------+----------------------------+
|   PaymentCode   | Debit | Credit |     PaymentDate     |         Particular         |
+-----------------+-------+--------+---------------------+----------------------------+
| CTM-41700000008 | 25000 |      0 | 2017-05-15 17:27:28 | Token Money From Customer1 |
| CTM-41700000007 | 12000 |      0 | 2017-05-15 17:26:26 | Token Money From Customer2 |
| CRV-11700000166 | 15000 |      0 | 2016-05-15 17:57:01 | Customer1 Receipt Vourcher |
| EPV-21700000012 |     0 |    150 | 2017-05-15 14:23:26 | Cash Outflow               |
| EPV-21700000004 |     0 |   1110 | 2017-05-15 14:06:48 | Cash Outflow               |
| EAS-41700000001 |     0 |  10000 | 2017-05-15 12:27:47 | Employee Advance Salary    |
+-----------------+-------+--------+---------------------+----------------------------+

Here is my query code in PHP 这是我的PHP查询代码

$branch_sql = "SELECT a.PaymentCode, a.Particular, a.Credit, a.Debit, 
Date(a.PaymentDate) AS PaymentDate, a.BranchID, a.CCode As RefPrint, 
a.RunningBalance, a.OpeningBalance, b.branchname FROM qry_ledger_all_balance AS a 
INNER JOIN tblbranches AS b 
ON a.BranchID = b.branchid 
WHERE DATE_FORMAT(a.PaymentDate, '%Y-%m-%d') >= '$Start_Date' AND
DATE_FORMAT(a.PaymentDate, '%Y-%m-%d') <= '$End_Date' 
ORDER BY a.PaymentDate ASC, a.Debit ASC, a.Credit ASC";

Edit 编辑

I suspect you want 我怀疑你想要

 ORDER BY DATE(a.PaymentDate), 
          CASE WHEN a.Debit > 0 THEN 1     /* nonzero debit */
               WHEN a.Credit > 0 THEN 2    /* zero debit, nonzero credit */
               ELSE 3 END,                 /* both zero, both nonzero */
          a.Debit, a.Credit

because if you don't use the DATE() function, MySQL will order by date and time. 因为如果您不使用DATE()函数,MySQL将按日期时间排序。

By the way, your date selection criteria aren't sargable . 顺便说一句,您的日期选择标准不可确定 This can cause serious performance problems when your tables get big. 当表变大时,这可能会导致严重的性能问题。

Try this instead. 试试这个吧。

WHERE a.PaymentDate  >= '$Start_Date'
  AND a.PaymentDate  < '$End_Date' + INTERVAL 1 DAY

(Notice the < and the extra day at the end of the range. That will include all records with DATETIME values anytime on the $End_Date . ) (请注意<和结束日期的额外天数。这将包括所有在$End_Date上随时具有DATETIME值的记录。)

If you have an index on a.PaymentDate this will allow that index to be range-scanned. 如果您在a.PaymentDate上具有索引,则将允许对该索引进行范围扫描。

Your order by clause needs to be slightly more clever. 您的order by子句需要更加巧妙。 Putting a case statement in your order by will let you create a group like sorting element. 将case语句按order by将使您创建一个类似于sorting element的组。

SELECT 
  a.PaymentCode, a.Particular, a.Credit, a.Debit, 
  Date(a.PaymentDate) AS PaymentDate, a.BranchID, 
  a.CCode As RefPrint, a.RunningBalance, 
  a.OpeningBalance, b.branchname 
 FROM qry_ledger_all_balance AS a 
 INNER JOIN tblbranches AS b 
   ON a.BranchID = b.branchid 
 WHERE DATE_FORMAT(a.PaymentDate, '%Y-%m-%d') 
   >= '$Start_Date' 
 AND DATE_FORMAT(a.PaymentDate, '%Y-%m-%d') 
   <= '$End_Date' 
 ORDER BY 
  date(a.PaymentDate) ASC, 
  case 
    when a.Debit > 0 
      then 0
    else 1
  end ASC, 
  case 
    when a.Debit > 0 
      then a.Debit
    else a.Credit
  end ASC

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

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