多表关联查询反面示例!

5个表的关联查询反面教材!

SELECT
            *
            FROM
            (
                SELECT
                    a.id,
                    a.useraccount,
                    a.REAL_NAME realName,
                    a.user_name userName,
                    a.buy_date buyDate,
                    a.address,
                    a.mobile,
                    a.createtime createTime,
                    a.is_operator isOperator,
                    a.is_shareholder isShareholder,
                    a.royalty_percent royaltyPercent,
                    (select real_name from table1 where id in (select agent_id  from table2 xs where xs.shareholder = a.id)) as agentName,
                    ( SELECT count( id ) FROM c_charging_order WHERE parkid IN ( SELECT id FROM table3 WHERE userId = a.id ) ) useCount,
                    ifnull(ob.amount,0) sum,
                    ob.company_code,
                    ob.bank_account,
                    ob.bank_name,
                    ob.bank_branch,
                    ( SELECT count( id ) FROM table3 WHERE userId = a.id ) pileCount,
                    ifnull(( SELECT sum( electric_charge ) FROM table3 p left join table4 ei on p.id = ei.charging_pile_id WHERE p.userId = a.id),0) totalPowerConsumption,
                    a.state
                FROM
                    table1 a
                    left join table5 ob ON a.id=ob.user_id
                WHERE
                    a.state IN (0, 1)
                    AND is_operator = 1
                    and a.device_type = #{p.platForm}
                    order by a.createtime desc
            ) a
        where 1=1
        <if test="p.keyWord!=null">
            and a.useraccount like  "%"#{p.keyWord}"%" or a.realName like "%"#{p.keyWord}"%"
            or a.mobile like  "%"#{p.keyWord}"%" or a.userName like "%"#{p.keyWord}"%"
        </if>

  

posted @ 2024-08-22 17:51  一木人生  阅读(8)  评论(0)    收藏  举报