How to join 4 tables in mysql PDO?












2















I have 4 tables as below:



1) courses



   | ID - > Primary Key | Name
-------------------------------
| 1 | Course 1
| 2 | Course 2
| 3 | Course 3


2) countries



   | IDPrimary Key | Name
-------------------------------
| 1 | Country 1
| 2 | Country 2
| 3 | Course 3


3) universities



   | ID - > Primary Key | Name           | country_id
---------------------------------------------------
| 1 | University 1 | 1
| 2 | University 2 | 1
| 3 | University 3 | 3


4) university_courses



   | ID - > Primary Key | university_id  | course_id
----------------------------------------------
| 1 | 1 | 2
| 2 | 3 | 2
| 3 | 3 | 3


Now, I need to create one REST API in core PHP for android app in that I will get two parameters country_id and course_id. Both will contain multiple values like country_id = "3,4,5" and course_id = "1,6,8".



I have to response all the related universities according to the country and course.



I have tried below query but i am not getting desired output so please help me if anyone have idea for my problem.



SELECT * 
FROM universities
LEFT JOIN countries ON countries.id = universities.country_id
LEFT JOIN university_courses ON universiity_courses.university_id = universities.id
LEFT JOIN courses ON courses.id = university_courses.course_id
WHERE FIND_IN_SET(universities.country_id, ?)
AND FIND_IN_SET(university_courses.course_id, ?)


If course_id = 1,2 and country_id = 2,3, my desired output should be:



| university_id |
| 3 |









share|improve this question




















  • 1





    You want to use the IN() operator

    – Raymond Nijland
    Jan 1 at 12:42













  • Any way we can use, but need proper output.

    – Jk7
    Jan 1 at 12:44











  • You are using LEFT JOIN which is wrong you need to use a INNER JOIN to get the related relations. Also you JOiN to much tables then you could. You only have to need to JOIN between university_courses and the universities to get the required results

    – Raymond Nijland
    Jan 1 at 12:52













  • Okay, now above issue is partially solved.

    – Jk7
    Jan 1 at 13:12











  • But now i m getting same university twice, if course_id=2,3. So in short Group by is not working.

    – Jk7
    Jan 1 at 13:15
















2















I have 4 tables as below:



1) courses



   | ID - > Primary Key | Name
-------------------------------
| 1 | Course 1
| 2 | Course 2
| 3 | Course 3


2) countries



   | IDPrimary Key | Name
-------------------------------
| 1 | Country 1
| 2 | Country 2
| 3 | Course 3


3) universities



   | ID - > Primary Key | Name           | country_id
---------------------------------------------------
| 1 | University 1 | 1
| 2 | University 2 | 1
| 3 | University 3 | 3


4) university_courses



   | ID - > Primary Key | university_id  | course_id
----------------------------------------------
| 1 | 1 | 2
| 2 | 3 | 2
| 3 | 3 | 3


Now, I need to create one REST API in core PHP for android app in that I will get two parameters country_id and course_id. Both will contain multiple values like country_id = "3,4,5" and course_id = "1,6,8".



I have to response all the related universities according to the country and course.



I have tried below query but i am not getting desired output so please help me if anyone have idea for my problem.



SELECT * 
FROM universities
LEFT JOIN countries ON countries.id = universities.country_id
LEFT JOIN university_courses ON universiity_courses.university_id = universities.id
LEFT JOIN courses ON courses.id = university_courses.course_id
WHERE FIND_IN_SET(universities.country_id, ?)
AND FIND_IN_SET(university_courses.course_id, ?)


If course_id = 1,2 and country_id = 2,3, my desired output should be:



| university_id |
| 3 |









share|improve this question




















  • 1





    You want to use the IN() operator

    – Raymond Nijland
    Jan 1 at 12:42













  • Any way we can use, but need proper output.

    – Jk7
    Jan 1 at 12:44











  • You are using LEFT JOIN which is wrong you need to use a INNER JOIN to get the related relations. Also you JOiN to much tables then you could. You only have to need to JOIN between university_courses and the universities to get the required results

    – Raymond Nijland
    Jan 1 at 12:52













  • Okay, now above issue is partially solved.

    – Jk7
    Jan 1 at 13:12











  • But now i m getting same university twice, if course_id=2,3. So in short Group by is not working.

    – Jk7
    Jan 1 at 13:15














2












2








2








I have 4 tables as below:



1) courses



   | ID - > Primary Key | Name
-------------------------------
| 1 | Course 1
| 2 | Course 2
| 3 | Course 3


2) countries



   | IDPrimary Key | Name
-------------------------------
| 1 | Country 1
| 2 | Country 2
| 3 | Course 3


3) universities



   | ID - > Primary Key | Name           | country_id
---------------------------------------------------
| 1 | University 1 | 1
| 2 | University 2 | 1
| 3 | University 3 | 3


4) university_courses



   | ID - > Primary Key | university_id  | course_id
----------------------------------------------
| 1 | 1 | 2
| 2 | 3 | 2
| 3 | 3 | 3


Now, I need to create one REST API in core PHP for android app in that I will get two parameters country_id and course_id. Both will contain multiple values like country_id = "3,4,5" and course_id = "1,6,8".



I have to response all the related universities according to the country and course.



I have tried below query but i am not getting desired output so please help me if anyone have idea for my problem.



SELECT * 
FROM universities
LEFT JOIN countries ON countries.id = universities.country_id
LEFT JOIN university_courses ON universiity_courses.university_id = universities.id
LEFT JOIN courses ON courses.id = university_courses.course_id
WHERE FIND_IN_SET(universities.country_id, ?)
AND FIND_IN_SET(university_courses.course_id, ?)


If course_id = 1,2 and country_id = 2,3, my desired output should be:



| university_id |
| 3 |









share|improve this question
















I have 4 tables as below:



1) courses



   | ID - > Primary Key | Name
-------------------------------
| 1 | Course 1
| 2 | Course 2
| 3 | Course 3


2) countries



   | IDPrimary Key | Name
-------------------------------
| 1 | Country 1
| 2 | Country 2
| 3 | Course 3


3) universities



   | ID - > Primary Key | Name           | country_id
---------------------------------------------------
| 1 | University 1 | 1
| 2 | University 2 | 1
| 3 | University 3 | 3


4) university_courses



   | ID - > Primary Key | university_id  | course_id
----------------------------------------------
| 1 | 1 | 2
| 2 | 3 | 2
| 3 | 3 | 3


Now, I need to create one REST API in core PHP for android app in that I will get two parameters country_id and course_id. Both will contain multiple values like country_id = "3,4,5" and course_id = "1,6,8".



I have to response all the related universities according to the country and course.



I have tried below query but i am not getting desired output so please help me if anyone have idea for my problem.



SELECT * 
FROM universities
LEFT JOIN countries ON countries.id = universities.country_id
LEFT JOIN university_courses ON universiity_courses.university_id = universities.id
LEFT JOIN courses ON courses.id = university_courses.course_id
WHERE FIND_IN_SET(universities.country_id, ?)
AND FIND_IN_SET(university_courses.course_id, ?)


If course_id = 1,2 and country_id = 2,3, my desired output should be:



| university_id |
| 3 |






php mysql pdo






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 13:24









RiggsFolly

71.4k1866113




71.4k1866113










asked Jan 1 at 12:38









Jk7Jk7

142




142








  • 1





    You want to use the IN() operator

    – Raymond Nijland
    Jan 1 at 12:42













  • Any way we can use, but need proper output.

    – Jk7
    Jan 1 at 12:44











  • You are using LEFT JOIN which is wrong you need to use a INNER JOIN to get the related relations. Also you JOiN to much tables then you could. You only have to need to JOIN between university_courses and the universities to get the required results

    – Raymond Nijland
    Jan 1 at 12:52













  • Okay, now above issue is partially solved.

    – Jk7
    Jan 1 at 13:12











  • But now i m getting same university twice, if course_id=2,3. So in short Group by is not working.

    – Jk7
    Jan 1 at 13:15














  • 1





    You want to use the IN() operator

    – Raymond Nijland
    Jan 1 at 12:42













  • Any way we can use, but need proper output.

    – Jk7
    Jan 1 at 12:44











  • You are using LEFT JOIN which is wrong you need to use a INNER JOIN to get the related relations. Also you JOiN to much tables then you could. You only have to need to JOIN between university_courses and the universities to get the required results

    – Raymond Nijland
    Jan 1 at 12:52













  • Okay, now above issue is partially solved.

    – Jk7
    Jan 1 at 13:12











  • But now i m getting same university twice, if course_id=2,3. So in short Group by is not working.

    – Jk7
    Jan 1 at 13:15








1




1





You want to use the IN() operator

– Raymond Nijland
Jan 1 at 12:42







You want to use the IN() operator

– Raymond Nijland
Jan 1 at 12:42















Any way we can use, but need proper output.

– Jk7
Jan 1 at 12:44





Any way we can use, but need proper output.

– Jk7
Jan 1 at 12:44













You are using LEFT JOIN which is wrong you need to use a INNER JOIN to get the related relations. Also you JOiN to much tables then you could. You only have to need to JOIN between university_courses and the universities to get the required results

– Raymond Nijland
Jan 1 at 12:52







You are using LEFT JOIN which is wrong you need to use a INNER JOIN to get the related relations. Also you JOiN to much tables then you could. You only have to need to JOIN between university_courses and the universities to get the required results

– Raymond Nijland
Jan 1 at 12:52















Okay, now above issue is partially solved.

– Jk7
Jan 1 at 13:12





Okay, now above issue is partially solved.

– Jk7
Jan 1 at 13:12













But now i m getting same university twice, if course_id=2,3. So in short Group by is not working.

– Jk7
Jan 1 at 13:15





But now i m getting same university twice, if course_id=2,3. So in short Group by is not working.

– Jk7
Jan 1 at 13:15












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995496%2fhow-to-join-4-tables-in-mysql-pdo%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995496%2fhow-to-join-4-tables-in-mysql-pdo%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules