mysql 子查询 将最外表带入子查询内2层 的另一种解决方法

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

SELECT (

            select count(*)  from  BORROW_LIST

             where DELETE_FLAG=0 and BORROW_ID=c.BORROW_ID 

               group by  LOAN_USER_ID

            ) as cyrs_COUNT,sum(...)  ....

                from  BORROW c

这样查出来的数量是多个结果,无法对应其他字段  ,并且数据 并不是总人数                                                                    

LOAN_USER_ID      USER_NAME     ..................

     1                            啊啊啊

    23                           啵啵啵

    23                           纯纯粹粹

    35                          点点滴滴

但是如果写成下面这样 的话 是无法取到c.BORROW_ID 的

SELECT (select count(*) from (

            select count(*)  from  BORROW_LIST

             where DELETE_FLAG=0 and BORROW_ID=c.BORROW_ID 

               group by  LOAN_USER_ID ) d

            ) as cyrs_COUNT,sum(...)  ....

                from  BORROW c

那么只有这样了~~~~~换个方法

SELECT (

            select count(distinct(LOAN_USER_ID))  from  BORROW_LIST

             where DELETE_FLAG=0 and BORROW_ID=c.BORROW_ID 

            ) as cyrs_COUNT,sum(...)  ....

                from  BORROW c

这样就不用再套一层子查询了


转载于:https://my.oschina.net/angleshuai/blog/632745

你可能感兴趣的:(mysql 子查询 将最外表带入子查询内2层 的另一种解决方法)