亚洲精品中文免费|亚洲日韩中文字幕制服|久久精品亚洲免费|一本之道久久免费

      
      

            <dl id="hur0q"><div id="hur0q"></div></dl>

                MySql小結(jié)

                需求1:何時(shí)用in,何時(shí)用exists查詢

                當(dāng)主表比從表大時(shí),IN查詢的效率較高,

                當(dāng)從表比主表大時(shí),EXISTS查詢的效率較高,

                in是先執(zhí)行子查詢,得到一個(gè)結(jié)果集,將結(jié)果集代入外層謂詞條件執(zhí)行主查詢,子查詢只需要執(zhí)行一次

                select phone,name from member t1where phone in(select phone from record t2 where win = true);

                exists是先從主查詢中取得一條數(shù)據(jù),再代入到子查詢中,執(zhí)行一次子查詢,判斷子查詢是否能返回結(jié)果,主查詢有多少條數(shù)據(jù),子查詢就要執(zhí)行多少次

                select phone,name from member t1 where exists(select 1 from record t2 where t1.phone=t2.phone and win =true);

                需求2:排行榜Top50(按分?jǐn)?shù)和獲取時(shí)間排名)

                set @rank = 0;

                select phone, name, (@r2:=@r2 +1) as rank

                from record

                order by score desc, createTime asc;

                需求3: 隨機(jī)數(shù), 將參與活動(dòng)的用戶,隨機(jī)抽取6個(gè)中獎(jiǎng)

                select * from momchilovtsi.mslaaccesslog order by RAND() limit 6;

                select min(id) ,max(id) momchilovtsi.mslaaccesslog

                需求4:獲取連續(xù)范圍的隨機(jī)數(shù): FLOOR(i + RAND() * (j – i + 1))

                隨機(jī)獲得 3333~9999的隨機(jī)數(shù)

                set @min = 3333;

                set @max = 9999;

                select FLOOR(@min+ (RAND() * (@max-@min+1)));

                需求5:刪除重復(fù)數(shù)據(jù)

                select * from msg a

                where id < (select max(id) from msg b

                where a.aid= b.aid

                and a.b_code=b.b_code

                and a.add_timestamp=b.add_timestamp)

                需求6: 列轉(zhuǎn)行統(tǒng)計(jì)

                set names utf8;

                select identity,

                sum(ct),

                sum(IF(channel = ‘1’, ct,0)) as channel_num_1,

                sum(IF(channel = ‘2’, ct,0)) as channel_num_2,

                sum(IF(channel = ‘3’, ct,0)) as channel_num_3,

                sum(IF(channel = ‘unknown’, ct,0)) as channel_num_unknown

                from (

                select identity, ifnull(channel,’unknown’) channel, count(1) ct

                from user group by identity,channel

                ) t

                group by identity

                需求7:逗號(hào)分隔的字符串分組統(tǒng)計(jì)

                格式:

                id

                value

                1

                1,2,3

                2

                1,2

                3

                3

                將列依據(jù)分隔符進(jìn)行分割,并得到列轉(zhuǎn)行的結(jié)果

                id

                value

                1

                1

                1

                2

                1

                3

                2

                1

                2

                2

                3

                3

                select * from name a;

                select * from squence b; // 序列表,只有一列id,存放1~100的數(shù)即可

                select a.id, substring_index(substring_index(a.answer,’,’,b.id),’,’,-1)

                from name a join squence b

                on b.id <= (length(a.answer) – length(replace(a.answer,',',''))+1)

                order by a.id, b.id;

                鄭重聲明:本文內(nèi)容及圖片均整理自互聯(lián)網(wǎng),不代表本站立場(chǎng),版權(quán)歸原作者所有,如有侵權(quán)請(qǐng)聯(lián)系管理員(admin#wlmqw.com)刪除。
                用戶投稿
                上一篇 2022年6月14日 18:09
                下一篇 2022年6月14日 18:09

                相關(guān)推薦

                聯(lián)系我們

                聯(lián)系郵箱:admin#wlmqw.com
                工作時(shí)間:周一至周五,10:30-18:30,節(jié)假日休息