douou2026 2013-07-19 05:18
浏览 31
已采纳

如何加入MySQL表

I need to add an additional join to a query but I'm not sure what to join it on.

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' 

I need to now join my url_alias table via a column value like:

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

-- EDITED TO CLARIFY --

This is for an OpenCart project that needs to simplify the url rewriting by adding the keyword to the existing category/product/manufacturer queries.

  • 写回答

1条回答 默认 最新

  • drvpvmz16241016 2013-07-19 05:35
    关注

    How about this:

    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' 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题