【问题标题】:How can I make custom SQL with order by in codeigniter如何在codeigniter中按顺序制作自定义SQL
【发布时间】:2017-12-07 06:23:42
【问题描述】:

我的代码在这里 Sql Query :

$sql ="select * from products where status='1' AND country='5' AND product_price > "50" AND product_price <= "100" order by product_id desc";
$product=$this->db->query($sql)->result_array();

使用海关 SQL 查看所有产品,但仍不按 desc 显示订单。

【问题讨论】:

  • 请发布您的表结构和数据以及预期的查询输出。
  • 我没有问题预期的查询输出,但问题是没有显示 desc 顺序。
  • 试试这个,选择 * from products where status='1' AND country='5' AND product_price > '50' AND product_price
  • select * from products where status='1' AND country='5' AND product_price > '50' AND product_price
  • 你能把查询的结果贴出来吗?

标签: mysql sql codeigniter codeigniter-3


【解决方案1】:

试试这个:

$sql = "SELECT * FROM `products` WHERE `status`=1 AND `country`=5 AND `product_price` > 50 AND `product_price` <= 100 ORDER BY `product_id` DESC";
        $product = $this->db->query($sql)->result_array();

【讨论】:

    【解决方案2】:

    最好使用内置的 Codeigniter Query-builder:

    $this->db->select('*');
    $this->db->where([
        'status'    => 1,
        'country'   => 5,
        'product_price >' => 50,
        'product_price <=' => 100,
    ]);
    $this->db->order_by('product_id', 'DESC');
    $this->db->get('products');
    $product = $this->db->result();
    

    【讨论】:

      【解决方案3】:

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

      $query = $this->db->query('select * from products where status='1' AND country='5' AND product_price > "50" AND product_price <= "100" order by product_id desc');
      $row_array = $query->result_array();
      

      我希望它会帮助你。

      【讨论】:

      • 好的,我会检查的。
      • 别工作!这个查询,其实这个查询和我的查询一样。
      • 请用每个字段的数据类型发布您的表结构
      【解决方案4】:

      试试这个

      $this->db->from('products');
      $this->db->where("status='1' AND country='5' AND product_price > '50' AND product_price <= '100'", null, false);
      $this->db->order_by("product_id", "desc");
      $query = $this->db->get(); 
      return $query->result_array();
      

      【讨论】:

      • 我要自定义查询。我不使用活动记录查询。
      【解决方案5】:

      请尝试以下代码:

      $this->db->select('products.*');
      $this->db->from('products');
      $sql="status='1' AND country='5' AND (product_price > '50' AND product_price <= '100')";
      $this->db->where($sql, NULL, FALSE)->order_by("product_id", "DESC");
      
      $products_query = $this->db->get();
      
      $products_info_array = array();
      
      $products_info_array = $products_query->result_array();
      
      return $products_info_array;
      

      【讨论】:

        【解决方案6】:

        你能“描述”输入表格吗?如果“product_id”字段是文本(例如:VARCHAR),那么它永远不会遵循 asc/desc.. 的顺序。

        对不起我的英语.. 干得好

        【讨论】:

        • product_id 字段为“int(11)、AUTO_INCREMENT 和主键”
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-31
        • 2019-09-03
        • 1970-01-01
        相关资源
        最近更新 更多