need help, how can we combine these query? ive a problem in join statement..
SELECT hc.id_company, hc.name, hc.email, dc.country, dc.date_join,
sum(if(ha.type='ADMIN',1,0)) as ADMIN,
sum(if(length(ha.type)=6,1,0)) as STAFF,
sum(if(ha.type='CUST',1,0)) as CUST
from h_company hc, d_company dc
JOIN h_adminstaffcust ha USING (id_company)
where hc.id_company=dc.id_company
group by id_company;
SELECT max(logs.date)
FROM h_company hc LEFT JOIN logs ON (hc.id_company = logs.id_company)
group by hc.id_company
having max(logs.date)<'2009-08-15';
thx2 for much help.. finally i modify the first query to eliminate first join. here is my combine code ...
SELECT
hc.ID_COMPANY,
hc.NAME,
hc.email,
(SELECT dc.country FROM d_company dc WHERE hc.ID_COMPANY = dc.ID_Company) as country,
(SELECT dc.date_join FROM d_company dc WHERE hc.ID_COMPANY = dc.ID_Company) as date_join,
max(logs.date),
(SELECT COUNT(ha.ID_COMPANY) FROM h_adminstaffcust ha WHERE hc.ID_COMPANY = ha.ID_Company and TYPE = 'ADMIN') as ADMIN,
(SELECT COUNT(ha.ID_COMPANY) FROM h_adminstaffcust ha WHERE hc.ID_COMPANY = ha.ID_Company and TYPE = 'CUST') as CUST,
(SELECT COUNT(ha.ID_COMPANY) FROM h_adminstaffcust ha WHERE hc.ID_COMPANY = ha.ID_Company and length(TYPE)=6) as STAFF
FROM h_company hc LEFT JOIN logs ON (hc.id_company = logs.id_company)
GROUP BY hc.ID_COMPANY
having max(logs.date)<'2009-08-15';