0

I am fairly new to MySQL, I wrote this query

SELECT 
        r.id as rID,
        r.title as rTitle,
        r.researcherName as rName,
        r.volumeID as rVolID,
        r.views as rViews,
        r.downloads as rdowns,
        vol.date as volDate,
        vol.title as volTitle,
        vol.versionID as verID,
        ver.title as verTitle
        FROM journalsResearches AS r 
        INNER JOIN  versions as ver
        INNER JOIN  volumes as vol
        ON r.volumeID = vol.id
        AND r.volumeID = 12
        ORDER BY r.views DESC

and expected result was 1 row and for some reason, this row has been duplicated as a result and still one row in table

screenshot

3
  • 1
    You're missing the ON condition for the versions table. Commented Dec 11, 2021 at 0:28
  • 1
    You're getting a cross product with every row in versions, the results have different verTitle. Commented Dec 11, 2021 at 0:29
  • There is no different title, the researches table has just one row. I'm also tried to add ON condition for the versions table, but the query return 3 row Commented Dec 11, 2021 at 17:02

2 Answers 2

1

You forgot a condition on the join with the 'versions' table, so your query might actually be returning one row per row in 'versions'.

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

1 Comment

Thanks, I fixed the query by rearranging tables in JOIN statement and adding ON condition on versions
1

Fixed with rearranging tables in JOIN statement and adding ON condition on versions

SELECT
        r.id as rID,
        r.title as rTitle,
        r.researcherName as rResearcherName,
        r.views as rViews,
        r.downloads as rDownloads,
        vol.id as volID,
        vol.title as volTitle,
        vol.date as volDate,
        ver.id as verID,
        ver.title as verTitle
        FROM
        journalsResearches AS r
        INNER JOIN  volumes as vol 
        INNER JOIN  versions as ver 
        ON r.volumeID = vol.id 
        AND vol.versionID = ver.id 
        AND vol.id = 12
        ORDER BY r.views DESC

Comments

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.