I have tables like this:
people
id_p name email phone
------------------------------------------
1 Wanda a@a.a +123
2 Vision b@b.b +234
3 Falcon c@c.c +345
4 Winter S d@d.d +456
lecturers
id_a id_p department field id_institution
-------------------------------------------------------------
1 1 Enginering Electronics 4
2 2 Life Science Psychology 12
students
id_b id_p major field id_institution
-------------------------------------------------------
1 3 Computer Robotic 1
2 4 Computer AI 5
I want to JOIN this to table to another table, this is query to get student:
SELECT people.* , student.major, student.field, another_table.some, another_table2.some FROM people
LEFT JOIN student ON student.id_p = people.id_p
LEFT JOIN another_table ON another_table.major = student.major
LEFT JOIN another_table2 ON another_table2.id_institution = student.id_institution
I wanna get the lecturer to, this is my query:
SELECT people.* , lecturer.department, lecturer.field, another_table.some, another_table2.some FROM people
LEFT JOIN lecturer ON lecturer.id_p = people.id_p
LEFT JOIN another_table ON another_table.departmen = lecturer.department
LEFT JOIN another_table2 ON another_table2.id_institution = lecturer.id_institution
These 2 queries is similiar. My real query is more complex than these queries, and have sub-queries. I use DataTable server-side because there is more than 10000 data to processed. But, even in server-side, it still take long time, and I think it is happend because there is two complex queries to be executed.
How to merge these 2 smiliar queries?