2

I've tried this code:

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

On SQL (and work well), and now I need this code to be implemented on codeigniter with query(). The problem is I can't insert

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

On query(), because it's always give error messages when I inserted it.

I have done this:

$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);

and I cant write anything before SELECT syntax because it will return error message when I try it.

3 Answers 3

8

You will have to run two seperate queries for this

$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);               
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, sir! I've tried this before and forgot to add first query(). And now it's work well! Thank you.
0

Create a stored procedure with your SQL and call the stored procedure from query()

2 Comments

I can't write any syntax before SELECT
The SET ... and your whole select statement would all be in the same stored procedure so your $query would be just be CALL YourSPName (mysql) or EXEC YourSPName (sql server)
0

Write in the model

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();
}

1 Comment

Please edit your answer to explain why this piece of code answers the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.