Mysql: 查詢函數 like , find_ in_set, regexp ,locate , instr

like  


select * from t_video where cateid like "%01%" or cateid like "%02%"

find_in_set( str, strlist )  

  -- 注:  返回值爲 str 首次 在strlist中出現的位置(下標從1開始) , 
  --      如果查不到返回0 
  --      strlist中每個元素以逗號分隔, 例:

 -- 例如:  
    -- 查cateid中包含01的, 而且,01下標爲2的    
    select TITLE from t_video where FIND_IN_SET( "01", CATEID)= 2 ;  結果爲: 天津市

    -- 查cateid中不包含01的
    select TITLE from t_video where FIND_IN_SET( "01", CATEID)= 0 ;
    
    
select * from t_video where FIND_IN_SET("01", CATEID) or FIND_IN_SET("02", CATEID);

SELECT
	t_v.*, GROUP_CONCAT(t_c.cate_name) 
    FROM t_video_category t_c, t_video t_v
    WHERE FIND_IN_SET(t_c.cate_id,t_v.cate_id) GROUP BY t_v.video_id

-- 注: 如果執行第二句sql出現以下問題
-- Expression #1 of SELECT list is not in GROUP BY clause and contains 
--nonaggregated column ********* which is not functionally dependent on 
--columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

--參考: https://www.cnblogs.com/xiangyuqi/p/10627274.html(雲數據庫直接在控制檯修改sql_model)

regexp(正則表達式)

select * from t_video where CATEID REGEXP(".*(01|02).*")

locate(substr, str, pos)

-- locate函數返回值:
--    substr: 條件    str: 字符串   pos: 從某個元素下標位置, 默認爲1
--    條件(substr)在字符串(st[pos:]r)中, 第一次出現的下標, 並且這個下標,  如果不存在, 則返回0

-- 例如: 河北省cateid中03的位置
select  locate("03", CATEID, 0) from t_video where id  = 3;    -- 返回值爲4
select  locate("103", CATEID) from t_video where id  = 3;      -- 返回值爲0

select * from t_video where locate("01", CATEID)  or locate("02", CATEID);

instr(str,substr)

-- 和locate一樣, 只不過兩個參數反過來了
select  INSTR(CATEID, "03") from t_video where id = 3;  -- 返回值爲4

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章