lifeson
lifeson

Reputation: 171

MySQL join multiple tables?

I have 3 tables

tblMarkers tblReviews tblImages

I want to return ALL the records from tblMarkers and a count of all the reviews and images for each marker. To complicate it further it should return all the markers within a radius of a known point.

so the expected result would be

MarkerID-MarkerName-ReviewCount-ImageCount

1223-SomeName-0-1

This is what I have so far and this returns a count of reviews, but I cant get the right SQL to add the count of images

SELECT
`tblMarkers`.`ID`,
`tblMarkers`.`Type`,
`tblMarkers`.`Name`,
`tblMarkers`.`Latitude`,
`tblMarkers`.`Longitude`, 
(3959 * acos( cos( radians('45.1') ) * cos( radians( Latitude ) ) * cos( radians( Longitude ) - radians('6') ) + sin( radians('45.1') ) * sin( radians( Latitude ) ) ) )  
AS distance,
Count(`tblReviews`.`marker_id`) As reviewCount
FROM
`tblMarkers`
LEFT JOIN `tblReviews` ON `tblMarkers`.`ID` = `tblReviews`.`marker_id`
GROUP BY
`tblMarkers`.`ID`,
`tblMarkers`.`Type`,
`tblMarkers`.`Name`,
`tblMarkers`.`Latitude`,
`tblMarkers`.`Longitude`
HAVING
`distance` < '50'
ORDER BY
distance;

The Images table structure is

ID [primaryKey] (same as tblMarkers.ID)
file
title

How do I add a count of all the images?

Upvotes: 0

Views: 94

Answers (2)

KaeL
KaeL

Reputation: 3659

Try this:

SELECT M.ID
    , M.Type
    , M.Name
    , M.Latitude
    , M.Longitude
    , (3959 * acos(cos(radians('45.1')) * cos(radians(M.Latitude )) * cos(radians(M.Longitude) - radians('6')) + sin(radians('45.1')) * sin(radians(M.Latitude ))))  AS distance
    , IFNULL(COUNT(DISTINCT R.review_id) , 0) AS ReviewCount
    , IFNULL(COUNT(DISTINCT I.ID), 0) AS ImageCount
    FROM tblMarkers AS M
    LEFT JOIN tblReviews AS R ON R.marker_id = M.ID
    LEFT JOIN tblImages AS I ON I.marker_id = M.ID
    GROUP BY M.ID, M.Type, M.Name, M.Latitude, M.Longitude
    HAVING distance < 50
    ORDER BY distance

I bet you already know LEFT JOIN. So I used COUNT(DISTINCT R.review_id)) to count all distinct review id's (just make sure that review_id is unique). In case there are no corresponding review and image record for a specific marker record, I used IFNULL(XXX , 0) to display 0.

I used ALIASES to make your query clean and neat. Feel free to ask.

Upvotes: 1

skparwal
skparwal

Reputation: 1084

IF ID field of image table is the reference of marker table ID field then the query will be:

SELECT
`tblMarkers`.`ID`,
`tblMarkers`.`Type`,
`tblMarkers`.`Name`,
`tblMarkers`.`Latitude`,
`tblMarkers`.`Longitude`, 
(3959 * acos( cos( radians('45.1') ) * cos( radians( Latitude ) ) * cos( radians( Longitude ) - radians('6') ) + sin( radians('45.1') ) * sin( radians( Latitude ) ) ) )  
AS distance,
Count(`tblReviews`.`marker_id`) As reviewCount,
Count(`tblImage`.`ID`) As imageCount,
FROM
`tblMarkers`
LEFT JOIN `tblReviews` ON `tblMarkers`.`ID` = `tblReviews`.`marker_id`
LEFT JOIN `tblImage` ON `tblMarkers`.`ID` = `tblImages`.`ID`
GROUP BY
`tblMarkers`.`ID`,
`tblMarkers`.`Type`,
`tblMarkers`.`Name`,
`tblMarkers`.`Latitude`,
`tblMarkers`.`Longitude`
HAVING
`distance` < '50'
ORDER BY
distance;

Upvotes: 0

Related Questions