You can do
Code:
select (select count(*)
from Post as P
where p.client_id = c.id
and p.status = 2) as notifications,
c.alias,
c.id,
cf.page_id as facebook_page_id,
cf.username as facebook_username,
ct.username as twitter_username,
cg.page_id as googleplus_page_id,
cg.username as googleplus_username
from Client as c
left
join Client_Facebook as cf
on cf.client_id = c.id
left
join Client_Twitter as ct
on ct.client_id = c.id
left
join Client_Googleplus as cg
on cg.client_id = c.id
where c.id = 18
or
Code:
select p.notifications,
c.alias,
c.id,
cf.page_id as facebook_page_id,
cf.username as facebook_username,
ct.username as twitter_username,
cg.page_id as googleplus_page_id,
cg.username as googleplus_username
from Client as c
left
join Client_Facebook as cf
on cf.client_id = c.id
left
join Client_Twitter as ct
on ct.client_id = c.id
left
join Client_Googleplus as cg
on cg.client_id = c.id
left
join (select count(*) as notifications,
client_id
from Post
where status = 2
group
by client_id) p
on p.client_id = c.id
where c.id = 18
In general the Mysql optimizer is very poor at handling subqueries, so the later query may be faster to execute.
Bookmarks