hello folks can anyone tell me what is this best way i mean any other way which will have less line of code as compare to this let me show query first
select agent.*, count(que.id) queue_count
from table agent
left join (select * from table2 q
INNER JOIN table3 p on q.path_id=p.id
where q.status=1 p.name='demo') que
on que.agent_id=agent.id
left join table3 path on path.id=que.path_id
group by agent.id
order by queue_count
i have 3 table now i want to join all table and inner select again im join 2 table ie table 2 and 3 with where clause
To get all information on an agent, then left-joining to get counts from another table, you would typically need to do the group by all non-aggregate fields... If your agent table has a bunch of fields, you would be better to pre-aggregate your sub-query to just get counts on a per-agentId basis... Then you have AT MOST one record per agent and can just simple LEFT-JOIN on that agent.
I assume your table references of "table", "table2", "table3" are actually fake name of actual tables, but using the ALIASES of "agent", "p", and "q" respectively within the query.
select
agent.*,
que.AgentQueueCount queue_count
from
table agent
left join
(select
q.agent_id,
count(*) AgentQueueCount
from
table2 q
INNER JOIN table3 p
on q.path_id = p.id
AND p.name='demo'
where
q.status = 1
group by
q.agent_id ) que
on agent.id = que.agent_id
order by
que.AgentQueueCount
Here, the sub-query is already inner-joined on the matching path ID and path name.
Remove table keyword and add AND in the condition.
select agent.id, count(que.id) queue_count
from agent
left join (select * from table2 q
INNER JOIN table3 p on q.path_id=p.id
where q.status=1 AND p.name='demo') que
on que.agent_id=agent.id
left join table3 path on path.id=que.path_id
group by agent.id
order by queue_count
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.