简体   繁体   中英

MySQL : LEFT JOIN for 4 tables which are connected to each other

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             |

UPDATE:

Above issue is solved but now facing new issue is getting same university multiple time in result like:

if course_id = 2,3 I am getting response as:

|university_id  |
------------------
|3              |
|3              |

For that I have used GROUP BY clause but getting 500 Internal Server Error if I remove GROUP BY It will work fine What can be the issue?

My new query is as below:

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

使用LEFT OUTER JOIN而不是LEFT JOIN

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM