Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql SQL使用2个左联接返回不正确的数据_Mysql - Fatal编程技术网

Mysql SQL使用2个左联接返回不正确的数据

Mysql SQL使用2个左联接返回不正确的数据,mysql,Mysql,我已经编写了一个MYSQL脚本,它返回不正确的数据。我精通SQL,但此查询没有返回正确的结果。有人能看看发生了什么事吗。问题是没有时间,没有时间。这两列的值相同,并且也是大值 select a.user_name as userName, coalesce(count(b.sp_user_name),0) as noOfBids, coalesce(ROUND(AVG(b.a_amount),2),0) as avgAmo

我已经编写了一个MYSQL脚本,它返回不正确的数据。我精通SQL,但此查询没有返回正确的结果。有人能看看发生了什么事吗。问题是没有时间,没有时间。这两列的值相同,并且也是大值

    select
           a.user_name as userName,
           coalesce(count(b.sp_user_name),0) as noOfBids,
           coalesce(ROUND(AVG(b.a_amount),2),0) as avgAmount,
           coalesce(count(d.sp_user_name),0) as noOfRatedTimes,
           coalesce(ROUND(AVG(d.user_rate),2),0)
    from users a
    left join project_imds b
       on b.sp_user_name = a.user_name
    left join projects c
       on b.project_code = c.project_code
    left join sp_user_rating d
       on d.sp_user_name = b.sp_user_name
    where a.user_type = 'SP'
    and a.active = 'Y'
    group by a.user_name
    order by coalesce(ROUND(AVG(d.user_rate),2),0) desc;

我已经创建了一个解决方法,通过创建一个临时表来获取平均值,并将其连接到主查询

由于我不知道您查询背后的具体数据,这只是一个猜测。但也许你更愿意将“sp_用户_评级”直接加入到“用户”中,改变

select
       a.user_name as userName,
       coalesce(count(b.sp_user_name),0) as noOfBids,
       coalesce(ROUND(AVG(b.a_amount),2),0) as avgAmount,
       coalesce(count(d.sp_user_name),0) as noOfRatedTimes,
       coalesce(ROUND(AVG(d.user_rate),2),0)
from users as a
left join project_imds as b
   on b.sp_user_name = a.user_name
left join projects as c
   on b.project_code = c.project_code
left join sp_user_rating as d
   on d.sp_user_name = b.sp_user_name
where a.user_type = 'SP'
and a.active = 'Y'
group by a.user_name
order by coalesce(ROUND(AVG(d.user_rate),2),0) desc;
left join sp_user_rating d
   on d.sp_user_name = b.sp_user_name


为什么这不同/帮助?我也看不出原始查询和此查询之间有任何区别?使用AS关键字添加到查询中
left join sp_user_rating d
   on d.sp_user_name = a.user_name