gpt4 book ai didi

php - 在 codeigniter 中使用 Order By

转载 作者:行者123 更新时间:2023-11-29 04:48:09 26 4
gpt4 key购买 nike

这是我的普通 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 查询中完成。但是,在事件记录中这样做会感到困惑。

最佳答案

试试这个:

            $this->db->select('i.id, condition, description, created_at, updated_at, category, latitude, longitude, status, d.device_token, d.facebook, d.preferred, d.email, d.phone');
$this->db->select("CONCAT('$url', `image_url_original`) AS `image_url_original`", false);
$this->db->select("CONCAT('$url', `image_url_mid`) AS `image_url_mid`", false);
$this->db->select("CONCAT('$url', `image_url_thumb`) AS `image_url_thumb`", false);
$this->db->select("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') AS `sort_col`", false);

$this->db->from('item as i');
$this->db->join('device as d', 'i.device_id = d.device_token');

$this->db->where('created_at > DATE_SUB(UTC_TIMESTAMP(), INTERVAL 24 HOUR)');
$this->db->where('i.status', 'shared');

if($language === 'nl_NL') {
$this->db->order_by('sort_col', 'asc');
} else {
$this->db->order_by('category', 'asc');
}

关于php - 在 codeigniter 中使用 Order By,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15585979/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com