我在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)
');
我收到一个错误,检查calc附近的sql语法。请解释问题的位置和原因。 p.s:我只是想按照这样的顺序使用calc:
$this->db->order_by('calc','desc');
$this->db->get();
更新:这是我在执行此查询时得到的:
Error Number: 1064
您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以便在'as calc)附近使用正确的语法FROM(products
)加入categories
ON categories
。id
= {{ 1}}。第1行的“ca”
答案 0 :(得分:1)
如果您要使用别名,那么您将不希望CodeIgnioter自动在列名称周围添加刻度,因为它会炸毁它们。只需将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) ;
答案 1 :(得分:0)
是的,你是对的@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) ;
也为我工作