๐ŸŒธ [SQL] | resultMap , IFNULL() , GROUP BY , JOIN

0
post-thumbnail

๐ŸŸฆ RESULTMAP

  • ์‚ฌ๋žŒ ์‚ฌ๋žŒ ๋งˆ๋‹ค ์‚ฌ์ง„์ฒฉ์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค
  • resultmap="id๊ฐ’"
  • ํ•œ๊ฐœ ๊ฐ€์ ธ์˜ฌ ๋–„๋Š” association
  • ์—ฌ๋Ÿฌ๊ฐœ ๊ฐ€์ ธ์˜ฌ ๋–„๋Š” collection

๐Ÿ”น type , id

  • ResultMap ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‹ค์Œ ์„ค์ •์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•œ๋‹ค.
    • type : ๋งคํ•‘๋  ํด๋ž˜์Šค๋ช…์„ ์ž‘์„ฑํ•œ๋‹ค.
    • id : ์™ธ๋ถ€์—์„œ ์ง€์ •ํ•  ์ด๋ฆ„์„ ์ž‘์„ฑํ•œ๋‹ค.

๐Ÿ”น result , column , property

  • <result> ํ•ญ๋ชฉ์„ ๋ฐฐ์น˜ํ•˜์—ฌ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์‹ค์ œ ๋งคํ•‘ ๊ด€๊ณ„๋ฅผ ์„ค์ •ํ•œ๋‹ค.
    • column : ๋ถˆ๋Ÿฌ์˜ฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ•ญ๋ชฉ๋ช…์„ ์ž‘์„ฑํ•œ๋‹ค.
    • property : ๋ถˆ๋Ÿฌ์˜จ ํ•ญ๋ชฉ์„ ์ €์žฅํ•  ํด๋ž˜์Šค ๋‚ด์˜ ๋ณ€์ˆ˜๋ช…์„ ์ž‘์„ฑํ•œ๋‹ค.

๐Ÿ”น <collection> , column , property , select

  • ๋งŒ์•ฝ ๋ฐ์ดํ„ฐ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ๋ผ๋ฉด ์ปฌ๋ ‰์…˜์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด ๋•Œ๋Š” <collection> ํ•ญ๋ชฉ์„ ๋ฐฐ์น˜ํ•˜์—ฌ ๋งคํ•‘ ๊ด€๊ณ„๋ฅผ ์„ค์ •ํ•œ๋‹ค.
    • column : ๋ถˆ๋Ÿฌ์˜ฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ•ญ๋ชฉ๋ช…์„ ์ž‘์„ฑํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ํ•˜์œ„ SQL์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ํ•ญ๋ชฉ์„ ์ž‘์„ฑํ•œ๋‹ค.(์ผ๋ฐ˜์ ์œผ๋กœ PK๋กœ ์ž‘์„ฑ)
    • property : ๋ถˆ๋Ÿฌ์˜จ ํ•ญ๋ชฉ์„ ์ €์žฅํ•  ํด๋ž˜์Šค ๋‚ด์˜ ๋ณ€์ˆ˜๋ช…์„ ์ž‘์„ฑํ•œ๋‹ค.
    • select : ์กฐํšŒํ•˜๋Š” ๊ตฌ๋ฌธ์ด ๋”ฐ๋กœ ํ•„์š”ํ•˜๋ฏ€๋กœ ํ•ด๋‹นํ•˜๋Š” ์กฐํšŒ ๊ตฌ๋ฌธ์˜ id๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.
    <resultMap id="FeedDomainMap" type="FeedDomain2">
        <result property="ifeed" column="ifeed"></result>
        <association property="cmt" column="ifeed" select="selFeedCmt"></association>
        <collection property="imgList" column="ifeed" select="selFeedImgList"></collection>
    </resultMap>

    <select id="selFeedList2" resultMap="FeedDomainMap">
        SELECT
            A.ifeed, A.location, A.ctnt, A.iuser, A.regdt
            , C.nm AS writer, C.mainProfile
            , IFNULL(E.cnt, 0) AS favCnt
        <if test="iuserForFav > 0">
            , CASE WHEN D.ifeed IS NULL THEN 0 ELSE 1 END AS isFav
        </if>
        FROM t_feed A
        INNER JOIN t_user C
        ON A.iuser = C.iuser
        <if test="iuserForMyFeed > 0">
            AND C.iuser = ${iuserForMyFeed}
        </if>
        LEFT JOIN (
            SELECT ifeed, COUNT(ifeed) AS cnt
            FROM t_feed_fav
            GROUP BY ifeed
        ) E
        ON A.ifeed = E.ifeed
        <if test="iuserForFav > 0">
        LEFT JOIN t_feed_fav D
        ON D.iuser = ${iuserForFav}
            AND A.ifeed = D.ifeed
        </if>
        ORDER BY A.ifeed DESC
        LIMIT #{startIdx}, #{limit}
    </select>

    <select id="selFeedCmt" resultType="FeedCmtDomain">
        SELECT A.*, COUNT(A.icmt) - 1 AS isMore
        FROM (
            SELECT
                A.icmt, A.cmt, A.regdt, A.ifeed
                , B.iuser, B.nm as writer, B.mainProfile as writerProfile
            FROM t_feed_cmt A
            INNER JOIN t_user B
            ON A.iuser = B.iuser
            WHERE A.ifeed = ${ifeed}
            ORDER BY icmt ASC
            LIMIT 2
        ) A
        GROUP BY A.ifeed
    </select>

    <select id="selFeedImgList" resultType="FeedImgEntity">
        SELECT ifeedimg, ifeed, img FROM t_feed_img
        WHERE ifeed = #{ifeed}
    </select>

๐ŸŸฆ IFNULL()

โœ… IFNULL(์ปฌ๋Ÿผ๋ช… , '์ปฌ๋Ÿผ์ด ๋„์ผ๊ฒฝ์šฐ ๋Œ€์ฒดํ•  ๋ฐ์ดํ„ฐ')

๐ŸŸฆ GROUP BY

  • ์ปฌ๋Ÿผ์— ๋ฐ์ดํ„ฐ๋ฅผ ๊ทธ๋ฃนํ™” ํ•  ์ˆ˜ ์žˆ๋Š” GROUP BY
  • ํŠน์ • ์ปฌ๋Ÿผ์„ ๊ทธ๋ฃนํ™” ํ•˜๋Š” GROUP BY
  • ํŠน์ • ์ปฌ๋Ÿผ์„ ๊ทธ๋ฃนํ™”ํ•œ ๊ฒฐ๊ณผ์— ์กฐ๊ฑด์„ ๊ฑฐ๋Š” HAVING
  • WHERE๋Š” ๊ทธ๋ฃนํ™” ํ•˜๊ธฐ ์ „์ด๊ณ , HAVING์€ ๊ทธ๋ฃนํ™” ํ›„์— ์กฐ๊ฑด

๐Ÿ”น ์ปฌ๋Ÿผ ๊ทธ๋ฃนํ™”

  • SELECT ์ปฌ๋Ÿผ FROM ํ…Œ์ด๋ธ” GROUP BY ๊ทธ๋ฃนํ™”ํ•  ์ปฌ๋Ÿผ;

๐Ÿ”น ์กฐ๊ฑด ์ฒ˜๋ฆฌ ํ›„์— ์ปฌ๋Ÿผ ๊ทธ๋ฃนํ™”

  • SELECT ์ปฌ๋Ÿผ FROM ํ…Œ์ด๋ธ” WHERE ์กฐ๊ฑด์‹ GROUP BY ๊ทธ๋ฃนํ™”ํ•  ์ปฌ๋Ÿผ;

๐Ÿ”น ์ปฌ๋Ÿผ ๊ทธ๋ฃนํ™” ํ›„์— ์กฐ๊ฑด ์ฒ˜๋ฆฌ

  • SELECT ์ปฌ๋Ÿผ FROM ํ…Œ์ด๋ธ” GROUP BY ๊ทธ๋ฃนํ™”ํ•  ์ปฌ๋Ÿผ HAVING ์กฐ๊ฑด์‹;

๐Ÿ”น ์กฐ๊ฑด ์ฒ˜๋ฆฌ ํ›„์— ์ปฌ๋Ÿผ ๊ทธ๋ฃนํ™” ํ›„์— ์กฐ๊ฑด ์ฒ˜๋ฆฌ

  • SELECT ์ปฌ๋Ÿผ FROM ํ…Œ์ด๋ธ” WHERE ์กฐ๊ฑด์‹ GROUP BY ๊ทธ๋ฃนํ™”ํ•  ์ปฌ๋Ÿผ HAVING ์กฐ๊ฑด์‹

๐Ÿ”น ORDER BY๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ

  • SELECT ์ปฌ๋Ÿผ FROM ํ…Œ์ด๋ธ” [WHERE ์กฐ๊ฑด์‹] GROUP BY ๊ทธ๋ฃนํ™”ํ•  ์ปฌ๋Ÿผ [HAVING ์กฐ๊ฑด์‹] ORDER BY ์ปฌ๋Ÿผ1 [, ์ปฌ๋Ÿผ2, ์ปฌ๋Ÿผ3 ...];

๐ŸŸฆ JOIN

โœ… join์„ ๋จผ์ € ํ•œ ํ›„์— select๋ฅผ ํ•œ๋‹ค

<select id="selUserFollowList" resultType="UserDomain">
    SELECT B.iuser, B.nm, B.mainProfile
    , CASE WHEN C.iuserMe IS NULL THEN 0 ELSE 1 END AS isMeFollowYou
    FROM t_user_follow A
    INNER JOIN t_user B
    ON A.iuserYou = B.iuser
    LEFT JOIN t_user_follow C
    ON C.iuserMe = ${iuserMe} -- ๋กœ๊ทธ์ธํ•œ ์‚ฌ๋žŒ pk
    AND C.iuserYou = A.iuserYou
    WHERE A.iuserMe = ${iuserYou} -- ํ”„๋กœํ•„ ์ฃผ์ธ์žฅ  pk
</select>
  • FROM follow๋ž‘ INNER JOIN user์˜ ๊ฒฐ๊ณผ๋ฅผ LEFT JOIN followํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ SELECTํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
profile
๋ช‡ ๋ฒˆ์„ ๋„˜์–ด์ ธ๋„ ์•ž์œผ๋กœ ๊ณ„์† ๋‚˜์•„๊ฐ€์ž

0๊ฐœ์˜ ๋Œ“๊ธ€