加入三个不同的mysql查询表

时间:2016-10-20 02:50:01

标签: mysql join mysqli inner-join mysqli-multi-query

Hai我是php和mysql的新手。这里想加入三个不同的quires。 这是我的疑问

查询1:

SELECT u.user_name,u.first_name 
FROM users u join users_cstm uc on u.id=uc.id_c 
WHERE u.deleted=0?

查询2:

SELECT l.assigned_user_id,count(*) AS lead_count 
FROM lead l GROUP BY l.assigned_user_id?

查询3:

SELECT l.assigned_user_id,AVG(DATEDIFF(l.date_modified,l.date_entered)) AS avgdays 
FROM leads l GROUP BY l.assigned_user_id?

所以.. .. ..

这是我正在寻找user_name,first_name,lead_count,avgdays的结果。来自三张桌子。

BigQuery join of three tables

我已经从上面的链接尝试了我的解决方案。但我没有得到结果?

感谢您的帮助和建议。

2 个答案:

答案 0 :(得分:0)

试试这个:

SELECT id,user_name,first_name,lead_count,avgdays from
(SELECT id,user_name,first_name,lead_count from
(SELECT u.id,u.user_name,u.first_name FROM users u,users_cstm uc where  u.id=uc.id_c and u.deleted=0) as a

LEFT JOIN

(SELECT l.assigned_user_id,count(*) AS lead_count FROM lead l GROUP BY l.assigned_user_id) as b
on a.id = b.assigned_user_id) as a

LEFT JOIN 

(SELECT l.assigned_user_id,AVG(DATEDIFF(l.date_modified,l.date_entered)) AS avgdays 
FROM leads l GROUP BY l.assigned_user_id) as b
on a.id = b.assigned_user_id

答案 1 :(得分:0)

Try below query:-

SELECT u.user_name,u.first_name,
lead_table.lead_count,lead_table.avgdays
FROM users u join users_cstm uc on u.id=uc.id_c
left join 
(SELECT l.assigned_user_id,
        AVG(l.date_modified) AS avgdays,
        count(*) AS lead_count 
FROM leads l GROUP BY l.assigned_user_id) lead_table
on u.id=lead_table.assigned_user_id
WHERE u.deleted=0;