我试图显示与许多表相关的表的结果,但我的问题是查询只返回一个特殊的产品我想返回的产品既是特殊的产品,也是非特殊产品。我的 PHP 查询是:$query = "SELECT DISTINCT p.product_id, p.price ,sp.date_end, f.percentage AS special_percentage , p.model, pd.name AS title, pd.description AS text, cd.name AS section, p.image, pd.tag, p.date_added AS created " ."FROM #__mijoshop_product AS p " ."JOIN #__mijoshop_product_special AS sp ON p.product_id = sp.product_id " ."JOIN #__mijoshop_flordeco_product_special_percentage AS f ON sp.product_id = f.product_id " ."INNER JOIN #__mijoshop_product_description AS pd ON p.product_id = pd.product_id " ."LEFT JOIN #__mijoshop_product_to_store AS ps ON p.product_id = ps.product_id " ."LEFT JOIN #__mijoshop_product_to_category AS pc ON p.product_id = pc.product_id " ."LEFT JOIN #__mijoshop_category_description AS cd ON (pc.category_id = cd.category_id AND cd.language_id = {$language_id}) " ."LEFT JOIN #__mijoshop_category_to_store AS cs ON (pc.category_id = cs.category_id AND cs.store_id = {$store_id}) " ."WHERE (LOWER(pd.name) LIKE '%" . $search_text . "%' OR LOWER(pd.description) LIKE '%" . $search_text . "%' OR LOWER(p.sku) LIKE '%" . $search_text . "%' OR "; if( $model ) { $query .= "LOWER(p.model) LIKE '%" . $search_text . "%' OR "; } $query .= "LOWER(pd.tag) LIKE '%" . $search_text . "%') " ."AND p.status = '1' " ."AND date(sp.date_end) >= date(NOW()) " ."AND p.date_available <= NOW() " ."AND ps.store_id = {$store_id} " ."AND pd.language_id = '" . $language_id . "' " ."GROUP BY p.product_id " ."ORDER BY {$order_by} " ."LIMIT ".$limit; $db->setQuery($query); $results = $db->loadObjectList();
1 回答
梦里花落0921
TA贡献1772条经验 获得超6个赞
更改与连接#__mijoshop_product_special
,并#__mijoshop_flordeco_product_special_percentage
以LEFT JOIN
使其不会结果仅限制于在此表的匹配产品。
此外,您在使用SELECT DISTINCT
时不需要使用GROUP BY p.product_id
; 由于每个产品 ID 只有 1 行,因此您无法获得任何重复项。但是,GROUP BY
当您不使用任何聚合函数(如SUM()
或 )时,使用它也没有意义COUNT()
。如果所有这些表都是 1 对 1 的对应关系,那么您不应该得到任何需要使用任一选项删除的重复项。
- 1 回答
- 0 关注
- 172 浏览
添加回答
举报
0/150
提交
取消