如何加入MySQL表

时间:2013-07-19 05:18:12

标签: php mysql opencart

我需要为查询添加一个额外的连接,但我不确定要加入它的是什么。

SELECT DISTINCT * FROM {$this->prefix}category c 
   LEFT JOIN {$this->prefix}category_description cd 
      ON (c.category_id = cd.category_id) 
   LEFT JOIN {$this->prefix}category_to_store c2s 
      ON (c.category_id = c2s.category_id) 
WHERE c.category_id = '" . (int)$category_id . "' 
   AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' 
   AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "' 
   AND c.status = '1' 

我现在需要通过列值加入我的url_alias表,如:

SELECT keyword FROM {$this->prefix}url_alias 
WHERE query = 'category_id=" . (int)$category_id . "'

- 编辑澄清 -

这适用于需要通过将关键字添加到现有类别/产品/制造商查询来简化网址重写的OpenCart项目。

1 个答案:

答案 0 :(得分:1)

这个怎么样:

SELECT DISTINCT * FROM {$this->prefix}category c 
  LEFT JOIN {$this->prefix}category_description cd 
    ON (c.category_id = cd.category_id) 
  LEFT JOIN {$this->prefix}category_to_store c2s 
    ON (c.category_id = c2s.category_id) 

  LEFT JOIN {$this->prefix}url_alias u 
    ON u.query = CONCAT('category_id=', c.category_id)

WHERE c.category_id = '" . (int)$category_id . "' 
  AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' 
  AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "' 
  AND c.status = '1'