[英]select products from specific category codeigniter
Products are available in following categories 提供以下类别的产品
product_id category_id
2 1,2
3 1,3
4 1,13
2 2,5
I want to select products of specific category eg all products from category 2. what will be sql syntax 我想选择特定类别的产品,例如类别2中的所有产品。什么是sql语法
You can use FIND_IN_SET
for seach in comma separated values. 您可以使用
FIND_IN_SET
来搜索逗号分隔的值。
$this->db->where("FIND_IN_SET( '$category_id' , category_ids) ");
So your whole query be like 所以你整个查询就像
$this->db->select();
$this->db->from('table_name');
$this->db->where("FIND_IN_SET( '$category_id' , category_ids) ");
$query = $this->db->get();
return $query->result();
You can try this FIND_IN_SET
for search comma separated values. 您可以尝试使用此
FIND_IN_SET
来搜索逗号分隔的值。
<?php
$this->db->select();
$this->db->from('table_name');
$this->db->where(' FIND_IN_SET(category_ids, "' . $category_ids . '") > 0', NULL, false);
$query = $this->db->get();
return $query->result();
?>
I hope it will help. 希望对您有所帮助。
You can use a simple SQL query & use this in your model. 您可以使用简单的SQL查询,并在模型中使用它。
public function get_product_category($category_id)
{
$query = "SELECT * FROM product_table WHERE category_id LIKE '%$categoryid%'";
return $this->db->query($query, $category_id)->result();
}
It will find in your product table where category_id
contain example 2
. 它将在您的产品表中找到
category_id
包含示例2
。
But, like M Khalid Junaid said, you better learn about database normalization. 但是,就像M Khalid Junaid所说的那样,您最好了解数据库规范化。
For example : Normalization Table 例如: 标准化表
This is your table before normalized, the product ID is duplicated on 2
how can if that is a primary key ? 这是归一化之前的表,产品ID重复
2
如果那是主键怎么办? So, try to change your table to normalized table, it will help you so much. 因此,尝试将您的表更改为规范化表,这将对您有很大帮助。
product_id category_id
2 1,2
3 1,3
4 1,13
2 2,5
This is a new table are normalized after creating a table named group_table
or whatever. 这是一个新表,在创建名为
group_table
或任何其他名称的表后将被规范化。 Then set group_id
as a primary_key
然后将
group_id
设置为primary_key
group_id product_id category_id
1 2 1
2 2 2
3 3 1
4 3 3
5 4 1
6 4 13
7 2 5
The query for find the product from category_id is more simple, just 从category_id查找产品的查询更简单,只是
SELECT * FROM group_table WHERE category_id = $category_id
or any condition that you wanted to create. 或您要创建的任何条件。 Like if you wanted to get product name just
JOIN
the table from product_table
就像您想获取产品名称一样,只需从
product_table
JOIN
表
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.