根据 Another Table 过滤查询获取数据

Filter qry to get data according to Another Table

这是获取数据库中所有可用类别的查询。

$sql = "SELECT cp.category_id AS category_id, GROUP_CONCAT(cd1.name ORDER BY cp.level SEPARATOR '  >  ') AS name, c1.parent_id, c1.sort_order 
    FROM " . DB_PREFIX . "category_path cp 
    LEFT JOIN " . DB_PREFIX . "category c1 ON (cp.category_id = c1.category_id) 
    LEFT JOIN " . DB_PREFIX . "category c2 ON (cp.path_id = c2.category_id) 
    LEFT JOIN " . DB_PREFIX . "category_description cd1 ON (cp.path_id = cd1.category_id) 
    LEFT JOIN " . DB_PREFIX . "category_description cd2 ON (cp.category_id = cd2.category_id) 
    WHERE cd1.language_id = '" . (int)$this->config->get('config_language_id') . "' AND 
    cd2.language_id = '" . (int)$this->config->get('config_language_id') . "'";

得到这个输出

So, i am trying to get only that categories listed in rma_mapping_category(this has category_id column)

所以我尝试了这个,但是通过添加这个(AND rma_mapping_category rma)语法出错了... ////////// //////////

$sqs = "SELECT cp.category_id AS category_id, GROUP_CONCAT(cd1.name ORDER BY cp.level SEPARATOR '  >  ') AS name, c1.parent_id, c1.sort_order 
        FROM " . DB_PREFIX . "category_path cp AND rma_mapping_category rma
        LEFT JOIN " . DB_PREFIX . "category c1 ON (cp.category_id = c1.category_id) 
        LEFT JOIN " . DB_PREFIX . "category c2 ON (cp.path_id = c2.category_id) 
  ->    LEFT JOIN " . DB_PREFIX . "category c3 ON (rma.categoryid = c3.category_id) 
        LEFT JOIN " . DB_PREFIX . "category_description cd1 ON (cp.path_id = cd1.category_id) 
        LEFT JOIN " . DB_PREFIX . "category_description cd2 ON (cp.category_id = cd2.category_id) 
        WHERE cd1.language_id = '" . (int)$this->config->get('config_language_id') . "' AND 
        cd2.language_id = '" . (int)$this->config->get('config_language_id') . "'";

也试过这个... //////////////////// 通过这个我得到了所有类别而不仅仅是 rma_mapping_category

与第一个查询相同的结果。

$sqs = "SELECT cp.category_id AS category_id, GROUP_CONCAT(cd1.name ORDER BY cp.level SEPARATOR '  >  ') AS name, c1.parent_id, c1.sort_order 
        FROM " . DB_PREFIX . "category_path cp
        LEFT JOIN " . DB_PREFIX . "category c1 ON (cp.category_id = c1.category_id) 
        LEFT JOIN " . DB_PREFIX . "category c2 ON (cp.path_id = c2.category_id) 
        LEFT JOIN rma_mapping_category rma ON (cp.category_id = rma.category_id) 
        LEFT JOIN " . DB_PREFIX . "category_description cd1 ON (cp.path_id = cd1.category_id) 
        LEFT JOIN " . DB_PREFIX . "category_description cd2 ON (cp.category_id = cd2.category_id) 
        WHERE cd1.language_id = '" . (int)$this->config->get('config_language_id') . "' AND 
        cd2.language_id = '" . (int)$this->config->get('config_language_id') . "'";

任何建议,我应该尝试什么?任何帮助将不胜感激。

您可以从递归查询开始。

举个例子:要获取 category_id 20 下的所有子类别,我将使用以下查询:

WITH RECURSIVE cte (category_id, path_id) AS (
  SELECT     category_id,
             path_id
  FROM       oc_category
  WHERE      path_id = 20
  UNION ALL
  SELECT     c.category_id,
             c.path_id
  FROM       oc_category c
  INNER JOIN cte
          ON c.path_id = cte.path_id
)
SELECT * FROM cte JOIN `oc_category_description` cd ON cd.category_id = cte.category_id;

另一个问题的 with keyword is what does the magic. Credit to this 回答在过去帮助我解决了这个问题。

添加 ==

后查询按预期工作

INNER JOIN rma_mapping_category rma ON (cp.category_id = rma.category_id) 喜欢下面

    $sql = "SELECT cp.category_id AS category_id, GROUP_CONCAT(cd1.name ORDER BY cp.level SEPARATOR '  >  ') AS name, c1.parent_id, rma.type_id, c1.sort_order 
    FROM " . DB_PREFIX . "category_path cp
    LEFT JOIN " . DB_PREFIX . "category c1 ON (cp.category_id = c1.category_id) 
    LEFT JOIN " . DB_PREFIX . "category c2 ON (cp.path_id = c2.category_id) 
    LEFT JOIN " . DB_PREFIX . "category_description cd1 ON (cp.path_id = cd1.category_id) 
    LEFT JOIN " . DB_PREFIX . "category_description cd2 ON (cp.category_id = cd2.category_id)
    INNER JOIN rma_mapping_category rma ON (cp.category_id = rma.category_id) 
    WHERE cd1.language_id = '" . (int)$this->config->get('config_language_id') . "' AND 
    cd2.language_id = '" . (int)$this->config->get('config_language_id') . "'";