2

我试过这段代码:

SET @num := 0, @type := NULL;
SELECT categories_name, products_name, products_url, categories_id, 
       @num := IF( @type = categories_name, @num +1, 1 ) AS row_number,
       @type := categories_name AS dummy
  FROM (
        SELECT categories_name, products_name, products_url, ptc.categories_id
          FROM toc_products_description pd
         INNER JOIN toc_products_to_categories ptc ON pd.products_id = ptc.products_id
         INNER JOIN toc_categories_description cd ON cd.categories_id = ptc.categories_id
               AND pd.language_id =1
               AND cd.language_id =1
        ) AS x
 GROUP BY x.categories_name, x.products_name, x.products_url, x.categories_id
HAVING row_number <=2

在 SQL 上(并且运行良好),现在我需要在 codeigniter 上使用query(). 问题是我不能插入

SET @num := 0, @type := NULL;

在 query() 上,因为当我插入它时它总是给出错误消息。

我已经这样做了:

$query="SELECT categories_name, products_name, products_url, categories_id, @num := IF( @type := categories_name, @num +1, TRUE) AS row_number, @type := categories_name AS dummy
                FROM (
                SELECT categories_name, products_name, products_url, ptc.categories_id
                FROM toc_products_description pd
                INNER JOIN toc_products_to_categories ptc ON pd.products_id = ptc.products_id
                INNER JOIN toc_categories_description cd ON cd.categories_id = ptc.categories_id
                AND pd.language_id =1
                AND cd.language_id =1
                ) AS x
                GROUP BY x.categories_name, x.products_name, x.products_url, x.categories_id
                HAVING row_number <=5";        
        $result = $this->db->query($query);

我不能在 SELECT 语法之前写任何东西,因为当我尝试它时它会返回错误消息。

4

3 回答 3

7

您将不得不为此运行两个单独的查询

$query  =   "SET @num := 0, @type := NULL";
$this->db->query($query);
$new_query  =   "
                SELECT categories_name, products_name, products_url, categories_id, 
                       @num := IF( @type = categories_name, @num +1, 1 ) AS row_number,
                       @type := categories_name AS dummy
                  FROM (
                        SELECT categories_name, products_name, products_url, ptc.categories_id
                          FROM toc_products_description pd
                         INNER JOIN toc_products_to_categories ptc ON pd.products_id = ptc.products_id
                         INNER JOIN toc_categories_description cd ON cd.categories_id = ptc.categories_id
                               AND pd.language_id =1
                               AND cd.language_id =1
                        ) AS x
                 GROUP BY x.categories_name, x.products_name, x.products_url, x.categories_id
                HAVING row_number <=2";
$this->db->query($new_query);               
于 2013-02-22T10:37:27.253 回答
0

使用您的 SQL 创建一个存储过程并从 query() 调用该存储过程

于 2013-02-22T07:41:01.037 回答
0

写入模型

public function select_datos($param1) {
    if ($param1 == 'planilla') {
        $query1 = "SET @numero := 0, @type := NULL";
        $this->db->query($query1);
        $this->db->select('*');
        $this->db->select("@numero:=@numero+1 AS 'posicion'");
        $this->db->from($param1);
        $query = $this->db->get();
        $row['planilla'] = $query->row();
    } else {
        $query = '1'; //    Error
    }
    return $query->result();
}
于 2018-02-26T15:42:59.230 回答