secondman
secondman

Reputation: 3277

How to Join MySQL Table

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.

Upvotes: 1

Views: 123

Answers (1)

Peter Gluck
Peter Gluck

Reputation: 8236

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' 

Upvotes: 1

Related Questions