views:

360

answers:

1

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';
+3  A: 

A few ways, but I'm not entirely sure what you're looking for to combine them. If you're looking to just return the companies which have a max log date less than 2009-08-15, you can use exists:

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
and exists (
    SELECT 1
    FROM h_company hc1 LEFT JOIN logs ON (hc1.id_company = logs.id_company)
    where hc1.id_company = hc.id_company
    group by hc1.id_company
    having coalesce(max(logs.date), '1900-01-01') < '2009-08-15'
)
group by id_company;

Also, note that I used coalesce to ensure you're capturing the null logs.

Now, if you want to see the last log date:

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)
inner join (SELECT hc1.id_company, max(logs.date) as maxdate
    FROM h_company hc1 LEFT JOIN logs ON (hc1.id_company = logs.id_company)
    group by hc1.id_company
    having coalesce(max(logs.date), '1900-01-01') < '2009-08-15')) as logs on
   hc.id_company = logs.id_company
where hc.id_company=dc.id_company

If you want to do this for just a specific company, do:

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)
inner join (SELECT hc1.id_company, max(logs.date) as maxdate
    FROM h_company hc1 LEFT JOIN logs ON (hc1.id_company = logs.id_company)
    where hc1.id_company = 1245 --Add where clause here
    group by hc1.id_company
    having coalesce(max(logs.date), '1900-01-01') < '2009-08-15')) as logs on
   hc.id_company = logs.id_company
where hc.id_company=dc.id_company

That way, you aren't returning the entirety of the list in that subquery.

Eric
i dont have any idea about exists. but it return error :#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'exists ( SELECT 1 FROM h_company hc1 LEFT JOIN logs ON (hc1.id_company =' at line 8
Daniel Budihardja
@Daniel: forgot the `and` :)
Eric
ya i already added 'and' but it return zero result.. hmm.. im using to make it per query and now cant combine it lol
Daniel Budihardja
@Daniel: My `group by` in the exists was using `hc` and not `hc1`. What does the inner query (minus the `where` line) return?
Eric
SELECT 1FROM h_company hc1 LEFT JOIN logs ON (hc1.id_company = logs.id_company)group by hc.id_companyhaving coalesce(max(logs.date), '1900-01-01') < '2009-08-15'sorry, this code you mean?but im lil bit confuse, how can i show the lastlogs field if i did ment it at main select? exist is about matching i think, isnt it?
Daniel Budihardja
Yeah, that's the query I mean. And exists works by seeing if any rows were returned for that specific query. It doesn't care what the content of those rows are. If you're trying to show the last logs of that field, then I'll add another query here to see if that will meet your needs.
Eric
+1 for the effort and thoroughness!
Roee Adler