联接未返回正确的结果.我究竟做错了什么? [英] Joins not returning correct results. What am I doing wrong?

查看:121
本文介绍了联接未返回正确的结果.我究竟做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行查询时,它仅通过对一行中的所有记录进行计数来给出一条记录,它应针对每个a.tech_id输出记录.为什么我的联接不起作用?

When I run my query it only gives one record by counting all the records in one row, it should output the records against each a.tech_id . Why my joins not working ?

select 
    concat(u.first_name, ' ', u.last_name) as name, 
    u.skill, 
    ut.name as team_name,
    (count(if(a.tech_id = u.user_id AND a.qc_id = 0 AND a.status = 2,1,0))) AS assigned_scopes,
    (count(if(a.tech_id = u.user_id  AND a.qc_id > 0 AND a.status = 3,1,0))) AS assigned_qa,
    (count(if(a.assignment_id=o.assignment_id and a.tech_id = u.user_id  AND a.status = 2 AND a.qc_id = 0 and o.class_id= 3,1,0))) AS assigned_canvass,
from am_ts_order o 
left join am_ts_assignment a
    ON a.assignment_id=o.assignment_id
left join am_user u
    ON a.tech_id=u.user_id
left join user_team ut
    ON u.user_team_id = ut.user_team_id
where u.user_role_id = 15
    and u.user_team_id = ut.user_team_id
order by u.first_name asc

推荐答案

在使用诸如count,sum等功能时,您需要添加group by子句来控制结果的分组:

When using functions like count, sum, etc., you need to add a group by clause to control the grouping of the results:

select 
    concat(u.first_name, ' ', u.last_name) as name, 
    u.skill, 
    ut.name as team_name,
    (count(if(a.tech_id = u.user_id AND a.qc_id = 0 AND a.status = 2,1,0))) AS assigned_scopes,
    (count(if(a.tech_id = u.user_id  AND a.qc_id > 0 AND a.status = 3,1,0))) AS assigned_qa,
    (count(if(a.assignment_id=o.assignment_id and a.tech_id = u.user_id  AND a.status = 2 AND a.qc_id = 0 and o.class_id= 3,1,0))) AS assigned_canvass,
from am_ts_order o 
left join am_ts_assignment a
    ON a.assignment_id=o.assignment_id
left join am_user u
    ON a.tech_id=u.user_id
left join user_team ut
    ON u.user_team_id = ut.user_team_id
where u.user_role_id = 15
    and u.user_team_id = ut.user_team_id
order by u.first_name asc
group by a.tech_id

更新:

此查询更改IF条件,以测试是否在某些左连接表中找到匹配的行:

This query alters the IF conditions to test if a matching row was found in some of the left joined tables:

select 
    concat(u.first_name, ' ', u.last_name) as name, 
    u.skill, 
    ut.name as team_name,
    count(if(
        (NOT a.tech_id IS NULL) AND 
        (NOT u.user_id IS NULL) AND 
        a.qc_id = 0 AND 
        a.status = 2,1,0)) AS assigned_scopes,
    count(if(
        (NOT a.tech_id IS NULL) AND 
        a.qc_id > 0 AND 
        a.status = 3,1,0)) AS assigned_qa,
    count(if(
        (NOT a.assignment_id IS NULL) AND
        a.assignment_id = o.assignment_id AND 
        a.tech_id = u.user_id AND 
        a.status = 2 AND 
        a.qc_id = 0 AND 
        o.class_id= 3,1,0)) AS assigned_canvass,
from am_ts_order o 
left join am_ts_assignment a
    ON a.assignment_id=o.assignment_id
left join am_user u
    ON a.tech_id=u.user_id
left join user_team ut
    ON u.user_team_id = ut.user_team_id
where u.user_role_id = 15
    and u.user_team_id = ut.user_team_id
order by u.first_name asc
group by a.tech_id

这篇关于联接未返回正确的结果.我究竟做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆