简体   繁体   中英

MySQL query with codeigniter (active record)

I am using following query in codeigniter using:

$this->db->select('products.id,
                   categories.name as cat_name, 
                   products.name as name,
                   products.product_image,
                   products.description,
                   products.price,
                   products.furl,
                   products.on_sale,
                   products.quantity_in_stock,
                   products.product_code,
                   products.rating_1,
                   products.rating_2,
                   products.rating_3,
                   products.rating_4,
                   products.rating_5,
                   products.rated_by,
                   products.discount,
                   ((products.rating_1+products.rating_2,
                     products.rating_3,
                     products.rating_4,
                     products.rating_5)/rated_by  as calc)
');

I get an error to check the sql syntax near calc. Please explain as to where and why the issue is. ps: I just wanted to use calc in order by clause like this:

$this->db->order_by('calc','desc');
$this->db->get();

UPDATE: This is what i get while executing this query:

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as calc) FROM ( products ) JOIN categories ON categories . id = products .`ca' at line 1

If you're going to use aliases then you'll not want to have CodeIgnioter automatically add ticks around your column names as it will blow things up. Just set the second paramter of select() to false to turn off this behaviour:

$this->db->SELECT(  
                 'products.id,categories.name as cat_name,  
                  products.name as name,  
                  products.product_image,  
                  products.description,  
                  products.price,  
                  products.furl,  
                  products.on_sale,  
                  products.quantity_in_stock,  
                  products.product_code,  
                  products.rating_1,  
                  products.rating_2,  
                  products.rating_3,  
                  products.rating_4,  
                  products.rating_5,  
                  products.rated_by,  
                  products.discount,  
                  ((products.rating_1+products.rating_2,  
                    products.rating_3,products.rating_4,  
                    products.rating_5)/rated_by  as calc)', false)  ;

yes you are correct @John

$this->db->select('products.id,categories.name as cat_name, products.name as name,products.product_image,products.description,products.price,products.furl,products.on_sale,products.quantity_in_stock,products.product_code,products.rating_1,products.rating_2,products.rating_3,products.rating_4,products.rating_5,products.rated_by,products.discount,((products.rating_1+products.rating_2,products.rating_3,products.rating_4,products.rating_5)/rated_by  as calc)', false)  ;

also worked for me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM