【问题标题】:How to order row by specific value? MySQL CodeIgniter如何按特定值排序行? MySQL CodeIgniter
【发布时间】:2018-06-26 09:25:38
【问题描述】:

我有一个问题。 当我尝试按特定值排序时,我的 sql 查询在 codeigniter 中不起作用 示例如下:

$this->db->select('*');
$this->db->from('Questions');
$this->db->where('Questions.Status !=', 0);
$this->db->order_by('IdQuestion', 'DESC');
$this->db->order_by('(CASE WHEN Status = 3 THEN 1 ELSE 2 END)', 'DESC'); //Here's wrong...

但我没有收到有效的结果。 有人可以帮忙。 第二个 order_by 语句是错误的。

CASE 工作不正确。

【问题讨论】:

  • @JayBlanchard 的问题不是一般如何排序,而是如何按值排序,这是CASE 的结果。
  • 啊 - 明白了@u_mulder。今天早上没有足够的咖啡和太多的冰。
  • 不确定 CodeIgniter 的细节,但尝试将 CASE 语句添加为具有别名的 SELECT 列,然后尝试按别名排序。
  • @flip 我想这是正确的方法。
  • @flip 谢谢,我会试试的。 ;)

标签: php mysql codeigniter


【解决方案1】:

您可以尝试这个解决方案来解决您的问题:

<?php 

$sub_query_from = '(SELECT Questions.*, (CASE WHEN Questions.Status = 3 THEN 1 ELSE 2 END) as questions_status from Questions  WHERE Questions.Status != 0 ORDER BY IdQuestion DESC) as sub_questions';
$this->db->select('sub_questions.*');
$this->db->from($sub_query_from);
$this->db->order_by('sub_questions.questions_status', 'DESC');
$query = $this->db->get();
$result = $query->result();

echo "<per>";
print_r($result);
exit;

?>

希望它会有所帮助。

【讨论】:

    【解决方案2】:

    我的解决方案是: 要创建包含所有排序结果的视图,请在 codeigniter 模型中选择此视图之前

    例子:

    select (case when (`parajurist`.`intrebari`.`Status` = 2) then 1 else 2 end) 
    AS `Second`,`parajurist`.`intrebari`.`IdIntrebare` AS 
    `IdIntrebare`,`parajurist`.`intrebari`.`Titlu` AS 
    `Titlu`,`parajurist`.`intrebari`.`Descriere` AS 
    `Descriere`,`parajurist`.`intrebari`.`NumePrenumeP` AS 
    `NumePrenumeP`,`parajurist`.`intrebari`.`EmailP` AS 
    `EmailP`,`parajurist`.`intrebari`.`Status` AS 
    `Status`,`parajurist`.`intrebari`.`IdCategorie` AS 
    `IdCategorie`,`parajurist`.`intrebari`.`DataAdresare` AS 
    `DataAdresare`,`parajurist`.`intrebari`.`Comments` AS 
    `Comments`,`parajurist`.`intrebari`.`CuvCheie` AS `CuvCheie` from 
    (`parajurist`.`intrebari` join `parajurist`.`intrebaricategorii` 
    on((`parajurist`.`intrebaricategorii`.`IdCategorie` = 
    `parajurist`.`intrebari`.`IdCategorie`))) where 
    (`parajurist`.`intrebari`.`Status` <> 0) order by (case when 
    (`parajurist`.`intrebari`.`Status` = 2) then 1 else 2 
    end),`parajurist`.`intrebari`.`IdIntrebare` desc
    

    和codeigniter代码:

    $this->db->limit($start, $stop);
    
        $this->db->select('*');
        $this->db->select('LEFT(intrebari_view.Titlu, 50) as Titlu');
        $this->db->select('LEFT(intrebari_view.Descriere, 150) AS Descriere');
        $this->db->join('IntrebariCategorii', 'IntrebariCategorii.IdCategorie = intrebari_view.IdCategorie');
        $this->db->where('IntrebariCategorii.NumeCategorie', $cat);
        $this->db->from('intrebari_view');
        $query = $this->db->get();
    

    【讨论】:

      猜你喜欢
      • 2012-01-09
      • 2014-08-17
      • 2014-02-04
      • 1970-01-01
      • 2020-03-08
      • 1970-01-01
      • 2016-12-25
      • 1970-01-01
      相关资源
      最近更新 更多