仅从 Api 中的产品 table 中获取特色产品
Fetch featured product only from product table in Api
产品Table
我只想获取特色产品。我在数据库 product_type 中定义了一个列,其中可以列出四种类型的产品(热卖,新上市,当日交易和特色产品)strong text 是可选。
块引用
Here is my model|query code in codeigniter
public function featured_product()
{
$arrResult = array();
$this->db->select('*');
$this->db->from('product');
$this->db->where("product_type","featured");
$result = $this->db->get()->result_array();
if(!empty($result))
{
$arrResult['result'] = $result;
}
else
{
$arrResult['result'] = '';
}
return $arrResult ;
}
当我尝试在 api 中获取整个产品列表时,我得到了结果,但我只想显示特色产品。
您可以尝试使用 find_in_set
:
https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_find-in-set
我不知道 codeigniter,但我认为这看起来很有用:
我提出一个不同的方法。
这不是您问题的答案,但我希望它能让您重新思考您的 table 结构。
不要将所有 product_types
存储在单个列中,而是使用 pivot table(也称为联结 table)。
您需要将当前在 product_types
列中的所有数据存储在单独的 table(数据透视表,我们称之为 product_to_type
)并引用 id
of product_table
and product_type
in the pivot table.
像这样:
这里有一个很好的db-fiddle,如果你愿意的话可以玩一下。
这有很多好处:
- 你有一个atomic tables
- 您可以将数据添加到任一 table 而不会在其他 table 中造成麻烦
- 外键确保数据一致性(你可能会做得更好。使用的密钥不错但不是很好)
- 您可以通过正确使用联接来提取任何类型的数据
- 您可以添加索引使查询更加顺畅
我相信还有其他优点。
因为我更改了我的数据库以将 table 与 product_id 与 product_type 匹配分开。我仍然通过函数 FIND_IN_SET()
.
找到了问题的解决方案
这是我更新的模态查询:
public function featured_product()
{
$arrResponse = array();
$data = 'featured';
$this->db->select('*');
$this->db->where('find_in_set("'.$data.'", product_type) <> 0');
$this->db->from('product');
$result = $this->db->get()->result_array();
if(!empty($result))
{
$arrResponse['result'] = $result;
}
else
{
$arrResponse['result'] = '';
}
return $arrResponse ;
}
产品Table
我只想获取特色产品。我在数据库 product_type 中定义了一个列,其中可以列出四种类型的产品(热卖,新上市,当日交易和特色产品)strong text 是可选。
块引用
Here is my model|query code in codeigniter
public function featured_product()
{
$arrResult = array();
$this->db->select('*');
$this->db->from('product');
$this->db->where("product_type","featured");
$result = $this->db->get()->result_array();
if(!empty($result))
{
$arrResult['result'] = $result;
}
else
{
$arrResult['result'] = '';
}
return $arrResult ;
}
当我尝试在 api 中获取整个产品列表时,我得到了结果,但我只想显示特色产品。
您可以尝试使用 find_in_set
:
https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_find-in-set
我不知道 codeigniter,但我认为这看起来很有用:
我提出一个不同的方法。
这不是您问题的答案,但我希望它能让您重新思考您的 table 结构。
不要将所有 product_types
存储在单个列中,而是使用 pivot table(也称为联结 table)。
您需要将当前在 product_types
列中的所有数据存储在单独的 table(数据透视表,我们称之为 product_to_type
)并引用 id
of product_table
and product_type
in the pivot table.
像这样:
这里有一个很好的db-fiddle,如果你愿意的话可以玩一下。
这有很多好处:
- 你有一个atomic tables
- 您可以将数据添加到任一 table 而不会在其他 table 中造成麻烦
- 外键确保数据一致性(你可能会做得更好。使用的密钥不错但不是很好)
- 您可以通过正确使用联接来提取任何类型的数据
- 您可以添加索引使查询更加顺畅
我相信还有其他优点。
因为我更改了我的数据库以将 table 与 product_id 与 product_type 匹配分开。我仍然通过函数 FIND_IN_SET()
.
这是我更新的模态查询:
public function featured_product()
{
$arrResponse = array();
$data = 'featured';
$this->db->select('*');
$this->db->where('find_in_set("'.$data.'", product_type) <> 0');
$this->db->from('product');
$result = $this->db->get()->result_array();
if(!empty($result))
{
$arrResponse['result'] = $result;
}
else
{
$arrResponse['result'] = '';
}
return $arrResponse ;
}