【发布时间】:2015-08-22 12:03:25
【问题描述】:
我想对产品进行数据库搜索,它有 6 个表,在我使用 product_subcategory 表之前一切正常, 它返回具有循环或重复行的相同 1 行(大约 250 行相同值,可能是因为子类别包含很多条目)。 product_subcategory 包含 subcategory_id、category_id、subcategory_name 列。产品子类别的信息以这种格式(15,24,1,3,8)作为 subcategory_id 存储在产品表中,(所以也许这就是我做错的地方)
我尝试了一些 IN 的变体,OR..但现在我卡住了..需要帮助 现在给出一个想法($branch = 'Tokyo' 和 $query = 'Mobiles'),许多子类别都包含单词 Mobiles..
我想要的最终结果是我可以使用返回结果
$sql = "SELECT t1.*, t2.*, t3.* , t4.*, t5.*, t6.*
FROM
products t1, product_details t2, product_warehouses t3, product_courier t4, product_category t5, product_subcategory t6
WHERE
t1.product_id = t2.product_id AND
t1.warehouse_id = t3.warehouse_id AND
t4.destination = t3.warehouse_branch AND
t1.category_id = t5.category_id AND
t3.warehouse_branch = ".$branch." AND
t5.category_name LIKE '%".$query."%' UNION
SELECT t1.*, t2.*, t3.* , t4.*, t5.*, t6.*
FROM
products t1, product_details t2, product_warehouses t3, product_courier t4, product_category t5, product_subcategory t6
WHERE
t1.product_id = t2.product_id AND
t1.warehouse_id = t3.warehouse_id AND
t4.destination = t3.warehouse_branch AND
t1.category_id = t5.category_id AND
t3.warehouse_branch = ".$branch." AND
t2.product_name LIKE '%".$query."%' UNION
SELECT t1.*, t2.*, t3.* , t4.*, t5.*, t6.*
FROM
products t1, product_details t2, product_warehouses t3, product_courier t4, product_category t5, product_subcategory t6
WHERE
t1.product_id = t2.product_id AND
t1.warehouse_id = t3.warehouse_id AND
t4.destination = t3.warehouse_branch AND
t1.category_id = t5.category_id AND
t3.warehouse_branch = ".$branch." AND
t1.product_title LIKE '%".$query."%' UNION
SELECT t1.*, t2.*, t3.* , t4.*, t5.*, t6.*
FROM
products t1, product_details t2, product_warehouses t3, product_courier t4, product_category t5, product_subcategory t6
WHERE
t1.product_id = t2.product_id AND
t1.warehouse_id = t3.warehouse_id AND
t4.destination = t3.warehouse_branch AND
t1.category_id = t5.category_id AND
t3.warehouse_branch = ".$branch." AND
t6.subcategory_id IN (t1.subcategory_id) AND //i think this is where im lost
t6.subcategory_name LIKE '%".$query."%'
";
当我从所有联合中删除子类别表时,我可以正确执行查询。 希望你们能帮帮我。
我使用 codeigniter 获得最终结果 $query = $this->db->query($sql); $result = $query->result_array();
【问题讨论】:
-
嗨..对不起,我应该提到这一点..我已经尝试过 UNION ALL 并且结果相同..还专门尝试了 UNION DISTINCT
标签: php mysql sql database codeigniter