【mysql】mysql查询 A表B表 1对多 统计A表对应B表中如果有对应,则返回true否则false作为A表查询结果返回

A表:goods_type

B表:brand_config

A:B = 1:N

一种商品类型 对应多条 品牌配置

========================================

需求:如果这个商品分类有对应的品牌配置,则返回字段值为true

========================================

示例代码:

<select
            id="findRoot2"
            parameterType="com.pisen.cloud.luna.ms.goods.base.domain.GoodsType"
            resultType="com.pisen.cloud.luna.ms.goods.base.domain.GoodsType">
        select
        g.create_date createDate,
        g.uid uid,
        g.name name,
        g.py_all pyAll,
        g.py_head pyHead,
        g.outer_id outerId,
        g.outer_code outerCode,
        g.mnemonic_code mnemonicCode,
        g.enabled_flag enabledFlag,
        g.remark remark,
        g.parent_uid parentUid,
        g.tenement_id tenementId,
        x.isconfig isconfig
        from
        goods_type g
        LEFT JOIN
        (SELECT CASE WHEN
        COUNT(id)>0
        THEN
        'true'
        ELSE
        'false'
        END
        isconfig,brand_uid from  brand_config GROUP BY brand_uid )    x ON x.brand_uid = g.uid
        where
        tenement_id = #{tenementId}
        and
        parent_uid IS NULL
        and
        del_flag = 0

        <if test="name != null and name != ''">
            AND
            name like '%' #{name} '%'
        </if>
        <if test="outerCode != null and outerCode != ''">
            AND outer_code = #{outerCode}
        </if>

    </select>
原文地址:https://www.cnblogs.com/sxdcgaq8080/p/9590565.html