简体   繁体   English

带codeigniter的MySQL查询(活动记录)

[英]MySQL query with codeigniter (active record)

I am using following query in codeigniter using: 我在codeigniter中使用以下查询:

$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. 我得到一个错误来检查calc附近的sql语法。 Please explain as to where and why the issue is. 请解释问题的位置和原因。 ps: I just wanted to use calc in order by clause like this: ps:我只是想按照这样的顺序使用calc:

$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; 您的SQL语法有错误; 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 . 检查与您的MySQL服务器版本对应的手册,以便在'as calc)附近使用正确的语法FROM( products )JOIN categories ON categories id = products .`ca' at line 1 第1行的id = products .`ca'

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. 如果您打算使用别名,那么您不希望CodeIgnioter自动在列名周围添加刻度,因为它会破坏。 Just set the second paramter of select() to false to turn off this behaviour: 只需将select()的第二个参数设置为false即可关闭此行为:

$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 是的,你是对的@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 也为我工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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