What am I doing wrong with this query? The results will sometimes return the same row.
Thanks!
SELECT
distinct studies.studyID,
studies.summary,
studies.link,
topics.topic
FROM
studies
LEFT join
studyTopics
ON
studyTopics.studyID = studies.studyID
LEFT JOIN
testimonialTopics
ON
testimonialTopics.topicID = studyTopics.topicID
LEFT JOIN
topics
ON
topics.topicID = studyTopics.topicID
LEFT JOIN
testimonials
ON
testimonials.testimonialID = testimonialTopics.testimonialID
WHERE
testimonials.testimonialID = '4353'
LIMIT
50;
what are you doing wrong? adding a WHERE condition on the right table in a LEFT OUTER JOIN
pls explain why you want LEFT OUTER JOINs – it seems to me that you want to start your FROM clause with the testimonials table, and build up from there using INNER JOINs
Also DISTINCT works on all columns selected and not just the one column. Just mentioning that as I’ve seen many posts where people do not understand that.
I’m sorry this query caused some confusion. Each testimonial is assigned multiple topics. Each scientific study is assigned multiple topics. I’m just trying to see a list of scientific studies that should be shown for a given testimonial. So if the testimonial is tagged with the topic of acne, and if there are 3 scientific studies that are also tagged with the topic of acne, then the results should show three rows. Currently the results will sometimes return the same scientific study more than once.
That was my thought as well. A testimonial may have the topic of acne, skin and blemishes. Some scientific studies may also match two or more of those same topics.
So what is the best way to ensure that only one scientific study is returned, even though it may match multiple topics? My attempt at using DISTINCT apparently did not work.
SELECT studies.studyID
, studies.summary
, studies.link
, GROUP_CONCAT(topics.topic) AS topics
FROM testimonialTopics
INNER
JOIN topics
ON topics.topicID = testimonialTopics.topicID
INNER
JOIN studyTopics
ON studyTopics.topicID = topics.topicID
INNER
JOIN studies
ON studies.studyID = studyTopics.studyID
WHERE testimonialTopics.testimonialID = 4353
GROUP
BY studies.studyID