SQL joins "going up" two tables

Posted by blcArmadillo on Stack Overflow See other posts from Stack Overflow or by blcArmadillo
Published on 2010-05-19T16:44:41Z Indexed on 2010/05/19 16:50 UTC
Read the original article Hit count: 346

Filed under:
|
|

I'm trying to create a moderately complex query with joins:

SELECT `history`.`id`,  
       `parts`.`type_id`, 
       `serialized_parts`.`serial`, 
       `history_actions`.`action`, 
       `history`.`date_added`
FROM `history_actions`, `history`
LEFT OUTER JOIN `parts` ON `parts`.`id` = `history`.`part_id`
LEFT OUTER JOIN `serialized_parts` ON `serialized_parts`.`parts_id` = `history`.`part_id`
WHERE `history_actions`.`id` = `history`.`action_id` 
  AND `history`.`unit_id` = '1' 
ORDER BY `history`.`id` DESC

I'd like to replace `parts`.`type_id` in the SELECT statement with `part_list`.`name` where the relationship I need to enforce between the two tables is `part_list`.`id` = `parts`.`type_id`. Also I have to use joins because in some cases `history`.`part_id` may be NULL which obviously isn't a valid part id. How would I modify the query to do this?

© Stack Overflow or respective owner

Related posts about sql

Related posts about left-join