I have a rather complicated query that I want to put into use with Zend Framework. In order to do this I needed to convert query from subqueries to joins.
This is the deal: I have 3 tables that need to be connected in order to form proper info. This can be accomplished using this query:
SELECT
ol.orders_id,
ol.photos_to_add,
ol.locations_id,
l.name,
(select
count(*)
from
photos as p
where
p.location_id = ol.locations_id and p.per_order = ol.orders_id)
as added
FROM `order_has_locations` AS ol,
`locations` AS l
WHERE ol.orders_id = 1 and l.id = ol.locations_id
I try to convert it to joins:
SELECT ol.orders_id, ol.locations_id, ol.photos_to_add, l.name, count(p.id) AS added
FROM order_has_locations as ol
INNER JOIN locations as l on l.id = ol.locations_id
left outer join photos as p on p.per_order = ol.orders_id
where p.location_id = ol.locations_id and ol.orders_id = 1
but this way the info about 0 photos is missing.
I tried many different approaches to no avail...