0

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?

1
  • as you mentioned about the image table structure, where is the relational field with the marker table?? without it you can not get the count Commented Jun 13, 2013 at 10:56

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

That compiled (using MYSQL front) but when I try to run it comes up with an error "Unknown column 'R.ID' in field list" link[/link]
I see, it is because there's no ID column in the tblReview table. Edited my answer.
Sorted now thanks - I had to remove the DISTINCT statement from the images list to get the number of images for each marker, but otherwise it works great!
0

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;

4 Comments

That's what I have tried but it does not run. What do you mean by 'reference marker'? (Told you I was new to this!
like you have reference id in reviews table. same you should have in image table
The ID in tblImages is the ID of the marker (does it matter they are the same name [ID], whereas in tblReviews the tblmarker ID is 'linked' to marker_id in tblReviews
as per your comment, above query will work. Please check it and let me know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.