这是我正常的mysql查询:
$sql = "SELECT * FROM item order by ";
if(my_condition)
{
$sql. = "FIELD(`category`, 'Auto & Accessories', 'Couch', 'Bed',
'Desk & Office', 'Bike & Scooter', 'Tools', 'Leisure',
'Iron & Wood', 'Cabinet', 'Kitchen & Accessories', 'Refrigerator & Appliances',
'Toys & Games', 'Chair', 'Table', 'Garden & Terrace', 'TV, HIFI & Computers',
'General Item')";
}
else
{
$sql .= "category asc";
}
我需要它在活动记录中的 CI 中。我尝试了以下方式:
if(my_condition)
{
$this->db->order_by("FIELD(`category`, 'Auto & Accessories', 'Couch',
'Bed', 'Desk & Office', 'Bike & Scooter', 'Tools', 'Leisure', 'Iron &
Wood', 'Cabinet', 'Kitchen & Accessories', 'Refrigerator &
Appliances', 'Toys & Games', 'Chair', 'Table', 'Garden & Terrace',
'TV, HIFI & Computers', 'General Item')");
}
else
{
$this->db->order_by("category", "asc");
}
但是,我得到了这个错误::
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 'Couch'`, `'Bed'`, `'Desk` &
Office', `'Bike` & Scooter', `'Tools'`, `'Leisure'`,' at line 6
Filename: C:\xampp\htdocs\straatjutter_service\system\database\DB_driver.php
Line Number: 330
如何解决?实际上,我需要按上述格式排序我的项目类别。这可以在正常的 mysql 查询中完成。但是,在活动记录中这样做会感到困惑。