我试过这段代码:
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 语法之前写任何东西,因为当我尝试它时它会返回错误消息。