I need some help guys, I have two tables,
(supervisor_id, name, area_specialty, branch)
, and(t_id, no_task, supervisor_id)
I'm trying to formulate a query that can filter supervisors with a given area of specialty
and from a given branch
.
After getting their IDs, I need to loop the IDs through the tasks table to get one ID with minimum tasks (no_task)
.
Below is what I have tried but it seems not to get me the correct results
$query2 = mysqli_query($connection, "SELECT * FROM supervisors WHERE area_specialty LIKE '%Embedded systems%' AND branch LIKE '%boston%'");
while($rows =mysqli_fetch_array($query2)) {
$sid =$rows['supervisor_id'];
$query1=mysqli_query($connection,"SELECT * from tasks INNER JOIN(select t_id, supervisor_id,MIN(no_task) AS nTask FROM tasks Group By supervisor_id) AS task_1 On task_1.t_id=task.t_id Where task_1.nTask=task.no_task");
$rm = mysqli_fetch_array($query1);
}
echo $rm['supervisor_id'];
Not sure why you are doing a loop when you can simply use some inner joins in your query. You will get performance issues later one when your tables are huge and you're looping those queries and connections. Without much details, I would do something like this:
select * from supervisor_table as s
inner join tasks_table as t
on t.supervisor_id = s.supervisor_id
where s.area_specialty like '%Embedded systems%' AND s.branch like '%boston%';
The query will give list of tasks as per your requirement:
SELECT t.* FROM task AS t
INNER JOIN (
SELECT MIN(no_task) AS MinTask FROM tasks Group By supervisor_id
INNER JOIN supervisor_table AS s ON t.supervisor_id = s.supervisor_id
WHERE area_specialty LIKE '%Embedded systems%' AND branch LIKE '%boston%'
) as Task1 ON Task1.MinTask = t.no_task
The sub query in the join will get minimum number of task for a supervisor with given area specialty and branch.
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.