MySQL中 標簽的用法


先給大家來個SQL語句:

choose (when,otherwize) ,相當於java 語言中的 switch ,與 jstl 中 的 choose 很類似。

<select id="getMemberInfo" resultType="java.util.Map" parameterType="java.util.Map" >
        SELECT
        att.address,
        m.id,
        m.sex AS sex_type,
        m.nickname,
        m.focus_num,        <!-- 關注的用戶數量 -->
        m.following_num,    <!-- 粉絲數量 -->
        m.consulation_num,    <!-- 收藏內容數量 -->
        m.user_type,        <!-- 0普通  1認證后用戶可以發布訪談 口述 -->
        <choose>
            <when test="login_member_id!=null">
            (select  count(*)  from alq_member_follow mf where  m.id=mf.member_id and  #{login_member_id}=mf.member_following_id and (mf.is_delete=0 or mf.is_delete is null)) as isFocus
            </when>

    <!-- when元素表示當 when 中的條件滿足的時候就輸出其中的內容,跟 JAVA 中的 switch
            效果差不多的是按照條件的順序,當 when 中有條件滿足的時候,就會跳出 choose,
            即所有的 when 和 otherwise 條件中,只有一個會輸出,當所有的我很條件都不滿足的時候就輸出
            otherwise 中的內容。所以上述語句的意思非常簡單, 當login_member_id!=null 的時候就輸出 isFocus,
            不再往下判斷條件,當所有條件都不滿足的時候就輸出 otherwise 中的內容。 -->
            <otherwise>
             0 as isFocus
            </otherwise>
        </choose>
        FROM `alq_member` m
        LEFT JOIN alq_attachment att ON att.id = m.logo_attachment_id
        WHERE m.id=#{member_id}
    </select>

這里面有 <choose>的用法,記錄一下,choose (when, otherwise)標簽

有時候我們並不想應用所有的條件,而只是想從多個選項中選擇一個。而使用if標簽時,只要test中的表達式為 true,就會執行 if 標簽中的條件。MyBatis 提供了 choose 元素。if標簽是與(and)的關系,而 choose 是或(or)的關系。

choose標簽是按順序判斷其內部when標簽中的test條件出否成立,如果有一個成立,則 choose 結束。當 choose 中所有 when 的條件都不滿則時,則執行 otherwise 中的sql。類似於Java 的 switch 語句,choose 為 switch,when 為 case,otherwise 則為 default。

這樣就很容易看懂了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM