mysql结合查询

时间:2009-08-24 03:58:17

标签: mysql

需要帮助,我们如何结合这些查询?我在连接声明中遇到了问题..

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提供了很多帮助.. 最后我修改了第一个查询以消除第一次加入。 这是我的组合代码......

   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';

1 个答案:

答案 0 :(得分:3)

有几种方法,但我不完全确定你想要将它们结合起来。如果您希望仅返回最大日志日期小于2009-08-15的公司,则可以使用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;

另请注意,我使用了coalesce来确保您捕获null日志。

现在,如果您想查看上次登录日期:

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

如果您只想为特定公司执行此操作,请执行以下操作:

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

这样,您就不会返回该子查询中的整个列表。