{"id":5530,"date":"2023-10-24T20:12:00","date_gmt":"2023-10-24T12:12:00","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=5530"},"modified":"2023-10-23T20:44:06","modified_gmt":"2023-10-23T12:44:06","slug":"hive-sql%e4%b8%ad%e7%9a%84like%e5%92%8crlike","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/5530.html","title":{"rendered":"Hive SQL\u4e2d\u7684like\u548crlike"},"content":{"rendered":"\n<p>=Start=<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7f18\u7531\uff1a<\/h4>\n\n\n\n<p>\u4ee5\u524d\u77e5\u9053SQL\u4e2d\u7684 like \u548c rlike \u662f\u6709\u533a\u522b\u7684\uff0c\u5dee\u522b\u4e3b\u8981\u5728\u4e8e\u524d\u8005\u53ea\u652f\u6301 \u767e\u5206\u53f7(%)\u2014\u2014\u5339\u914d\u4efb\u610f\u6570\u91cf\u7684\u4efb\u610f\u5b57\u7b26\uff0c\u548c\u4e0b\u5212\u7ebf(_)\u2014\u2014\u5339\u914d\u4e00\u4e2a\u4efb\u610f\u5b57\u7b26 \u4f5c\u4e3a\u7279\u6b8a\u5b57\u7b26\uff0c\u540e\u8005\u652f\u6301\u6b63\u5219\u5339\u914d\u2014\u2014\u529f\u80fd\u66f4\u5f3a\u5927\uff0c\u4f46\u901f\u5ea6\u4e00\u822c\u4e5f\u8f83\u6162\u3002\u6240\u4ee5\u6211\u4e00\u822c\u662f\u7b80\u5355\u7684\u3001\u5e0c\u671b\u901f\u5ea6\u5feb\u4e9b\u7684\u60c5\u51b5\u4e0b\u7528like\u505a\u6a21\u7cca\u5339\u914d\uff0c\u5176\u5b83\u573a\u666f\u7528rlike\u5b9e\u73b0\u3002<br>\u4f46\u662f\u8fd1\u671f\u5728\u5206\u6790\u65e5\u5fd7\u7684\u65f6\u5019\u53d1\u73b0Hive SQL\u4e2d\u7684 like \u548c rlike \u9664\u4e86\u5728\u529f\u80fd\u4e0a\u6709\u533a\u522b\u4e4b\u5916\uff0c\u8fc7\u6ee4\u751f\u6210\u7684\u7ed3\u679c\u4e5f\u6709\u5dee\u5f02\uff0c\u6bd4\u8f83\u5947\u602a\uff0c\u5728\u6b64\u8bb0\u5f55\u4e00\u4e0b\uff0c\u65b9\u4fbf\u540e\u9762\u53c2\u8003\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b63\u6587\uff1a<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n\n\n\n<p>\u76f8\u5173\u80cc\u666f\uff1a<br>\u5f53\u524d\u6709\u4e00\u4e2a\u6e05\u6d17\u597d\u7684SQL\u67e5\u8be2\u65e5\u5fd7\uff0c\u5176\u4e2d\u9664\u4e86\u5b8c\u6574\u7684\u67e5\u8be2\u8bed\u53e5\u5b57\u6bb5 query \u4e4b\u5916\uff0c\u8fd8\u6709\u901a\u8fc7\u5185\u5bb9\u626b\u63cf\u63d0\u53d6\u51fa\u7684\u67e5\u8be2\u8868\u540d table_name \u548c\u8be5\u8868\u88ab\u6807\u8bb0\u6709\u654f\u611f\u6570\u636e\u7684\u5217\u540d col_name \uff0c\u6211\u4eec\u5728\u5206\u6790\u7684\u65f6\u5019\u9664\u4e86\u4f1a\u5224\u65ad\u662f\u5426\u5bf9\u654f\u611f\u8868\u505a\u67e5\u8be2\u4e4b\u5916\uff0c\u8fd8\u9700\u8981\u5224\u65ad\u662f\u5426\u67e5\u8be2\u4e86\u654f\u611f\u5217\uff08\u4ee5\u53ca\u6240\u6709\u5217\uff09\uff0c\u5426\u5219\u4ec5\u4ec5\u53ea\u9488\u5bf9\u654f\u611f\u8868\u7684\u67e5\u8be2\u64cd\u4f5c\u53ef\u80fd\u6ca1\u90a3\u4e48\u654f\u611f\uff08\u5bf9\u4e8e\u7edf\u8ba1\u7c7b\u64cd\u4f5c\u4f1a\u5728\u53e6\u5916\u7684\u4e00\u4e2a\u5206\u6790\u7ef4\u5ea6\u53bb\u505a\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u5148\u8bf4\u7ed3\u8bba\uff0c\u7528 rlike\/regexp \u6765\u8fdb\u884c\u5185\u5bb9\u5339\u914d\uff0c\u6ca1\u4e8b\u522b\u7528 like \u505a\u5185\u5bb9\u5339\u914d\u5224\u65ad\u4e86\uff0c\u7ed3\u679c\u53ef\u80fd\u4e0d\u7b26\u5408\u9884\u671f\n\n,if(query rlike concat('\\\\s{0,},\\\\s{0,}',col_name),1,0) as comma_left\n,if(query rlike concat(col_name,'\\\\s{0,},\\\\s{0,}'),1,0) as comma_right\n,if((lower(query) like '%select *%' or query rlike ',\\\\s{0,}\\\\*'),1,0) as select_all\n\n-- where \u6761\u4ef6\u4e2d\uff0c\u9759\u6001\u7684\u53ef\u4ee5\u7528 like \u6765\u8fc7\u6ee4\uff0c\u52a8\u6001\u751f\u6210\u7684\u8fd8\u662f\u9700\u8981\u7528 rlike \u6765\u8fc7\u6ee4\uff08\u907f\u514d\u6f0f\u8fc7\uff09\uff1b\nand query rlike concat(',',col_name) -- \u5efa\u8bae\u7528-rlike-\u53ef\u4ee5\u5339\u914d\u523098\u6761\u8bb0\u5f55\nand query like concat('%,',col_name,'%') -- \u4e0d\u5efa\u8bae-like-\u53ef\u4ee5\u5339\u914d\u523050\u6761\u8bb0\u5f55\n\nand query like '%hello%' -- \u8fd9\u91cchello\u7684\u5185\u5bb9\u662f\u56fa\u5b9a\u7684\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528like\u8fdb\u884c\u5339\u914d\nand query like concat('%,','hello','%') -- \u548c\u4e0a\u9762\u7684\u529f\u80fd\u7b49\u4ef7<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u683c\u5f0f A like B \uff0c\u5176\u4e2dA\u662f\u5b57\u7b26\u4e32\uff0cB\u662f\u8868\u8fbe\u5f0f\uff0c\u8868\u793a<strong>\u80fd\u5426\u7528B\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u53bb\u5b8c\u5168\u5339\u914dA\u8fd9\u4e2a\u5b57\u7b26\u4e32\u7684\u5185\u5bb9<\/strong>\uff0c\u6362\u53e5\u8bdd\u8bf4<strong>\u80fd\u5426\u7528B\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u53bb\u8868\u793aA\u7684\u5168\u90e8\u5185\u5bb9<\/strong>\uff0c\u6ce8\u610f\u8fd9\u4e2a\u548crlike\u662f\u6709\u533a\u522b\u7684\u3002\u8fd4\u56de\u7684\u7ed3\u679c\u662fTrue\/False\u3002<\/p>\n\n\n\n<p>B\u53ea\u80fd\u4f7f\u7528\u7b80\u5355\u5339\u914d\u7b26\u53f7<em>\u548c%\uff0c\u201d<\/em>\u201d\u8868\u793a\u4efb\u610f\u5355\u4e2a\u5b57\u7b26\uff0c\u5b57\u7b26\u201d%\u201d\u8868\u793a\u4efb\u610f\u6570\u91cf\u7684\u5b57\u7b26\u3002<\/p>\n\n\n\n<p>like\u7684\u5339\u914d\u662f\u6309\u5b57\u7b26\u9010\u4e00\u5339\u914d\u7684\uff0c\u4f7f\u7528B\u4eceA\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u5f00\u59cb\u5339\u914d\uff0c\u6240\u4ee5\u5373\u4f7f\u6709\u4e00\u4e2a\u5b57\u7b26\u4e0d\u540c\u90fd\u4e0d\u884c\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u5b57\u7b26\u4e32A\u6216\u8005\u5b57\u7b26\u4e32B\u4e3aNULL\uff0c\u5219\u8fd4\u56deNULL\uff1b\u5982\u679c\u5b57\u7b26\u4e32A\u7b26\u5408\u8868\u8fbe\u5f0fB\u7684\u6b63\u5219\u8bed\u6cd5\uff0c\u5219\u4e3aTRUE\uff1b\u5426\u5219\u4e3aFALSE\u3002\u5c24\u5176\u6ce8\u610fNULL\u503c\u7684\u5339\u914d\uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u4e0d\u662fFALSE\u548cTRUE\uff0c\u800c\u662fnull\uff0c\u5176\u5b9e\u9664\u4e86is null\/is not null\uff0c\u5176\u4ed6\u7684\u5173\u7cfb\u8fd0\u7b97\u7b26\u53ea\u8981\u78b0\u5230null\u503c\u51fa\u73b0\uff0c\u7ed3\u679c\u90fd\u662f\u8fd4\u56deNULL\uff0c\u800c\u4e0d\u662fTRUE\/FALSE\u3002<\/p>\n<\/blockquote>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>select\n'abcde' like 'abc' -- false\n,'abcde' like 'abcde' -- true\n,'abcde' like 'abc%' -- true\n,'abcde' like 'abcde%' -- true\n,'abcde' like '%abcde%' -- true\n;\n\nselect\nnull like '%'\n,NULL like '%'\n,'abc' like null\n,'abc' like NULL\n,'abcde' like '%abcde%'\n\/*\n_c0    _c1 _c2 _c3 _c4 \n                true\n*\/\n\n\nselect\n'abc' like 'abcde' -- false\n,'abc' like 'abc%' -- true\n,'abc' like 'abcde%' -- false\n,'abc' like '%abc%' -- true\n;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/*\n\u5206\u6790\u76ee\u6807\u662f\u627e\u51fa\u67e5\u8be2SQL\u4e2d\u5305\u542b\u654f\u611f\u5217\u5173\u952e\u5b57\u7684\u67e5\u8be2\u884c\u4e3a\nquery - \u5b8c\u6574\u67e5\u8be2\u8bed\u53e5\u5b57\u6bb5\ncol_name - \u654f\u611f\u5217\u540d\u5b57\u6bb5\n*\/\nSELECT\n  col_name,\n  if(regexp_replace(query,'\\\\s{0,},\\\\s{0,}',',') like concat('%',col_name,'%'),1,0) as col_in,\n  if(regexp_replace(query,'\\\\s{0,},\\\\s{0,}',',') like concat('%,',col_name,'%'),1,0) as comma_left,\n  if(regexp_replace(query,'\\\\s{0,},\\\\s{0,}',',') like concat('%',col_name,',%'),1,0) as comma_right,\n\n  if(regexp_replace(query,'\\\\s{0,},\\\\s{0,}',',') rlike col_name,1,0) as col_in2, --\u5b9e\u9645\u60c5\u51b5\u7b26\u5408\u9884\u671f\n  if(regexp_replace(query,'\\\\s{0,},\\\\s{0,}',',') rlike concat(',',col_name),1,0) as comma_left2, --\u5b9e\u9645\u60c5\u51b5\u7b26\u5408\u9884\u671f\n  if(regexp_replace(query,'\\\\s{0,},\\\\s{0,}',',') rlike concat(col_name,','),1,0) as comma_right2, --\u5b9e\u9645\u60c5\u51b5\u7b26\u5408\u9884\u671f\n\n  regexp_replace(query,'\\\\s{0,},\\\\s{0,}',',') as query1,\n  query\nFROM\n  hive_db.query_log\nWHERE\n  p_date BETWEEN '20231015' and '20231015'\n-- and regexp_replace(query,'\\\\s{0,},\\\\s{0,}',',') like concat('%,',col_name,'%') -- like-\u53ef\u4ee5\u5339\u914d\u523050\u6761\u8bb0\u5f55\nand regexp_replace(query,'\\\\s{0,},\\\\s{0,}',',') rlike concat(',',col_name) -- rlike-\u53ef\u4ee5\u5339\u914d\u523098\u6761\u8bb0\u5f55\nLIMIT 100<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n\n\n\n<p>Hive\u4e2drlike,like,not like\uff0cregexp\u533a\u522b\u4e0e\u4f7f\u7528\u8be6\u89e3<br><a href=\"https:\/\/blog.csdn.net\/qq_26442553\/article\/details\/79452221\">https:\/\/blog.csdn.net\/qq_26442553\/article\/details\/79452221<\/a><\/p>\n\n\n\n<p>[\u77e5\u8bc6\u8bb2\u89e3\u7bc7-20]Hive\u7684\u51e0\u79cdlike\u8be6\u89e3<br><a href=\"https:\/\/zhuanlan.zhihu.com\/p\/337885986\">https:\/\/zhuanlan.zhihu.com\/p\/337885986<\/a><\/p>\n\n\n\n<p>HIVE\u5e38\u7528\u6b63\u5219\u51fd\u6570(like\u3001rlike\u3001regexp\u3001regexp_replace\u3001regexp_extract)<br><a href=\"https:\/\/www.jianshu.com\/p\/3bcc06b1294b\">https:\/\/www.jianshu.com\/p\/3bcc06b1294b<\/a><\/p>\n\n\n\n<p>hive like \u8bed\u6cd5<br><a href=\"https:\/\/juejin.cn\/s\/hive%20like%20%E8%AF%AD%E6%B3%95\">https:\/\/juejin.cn\/s\/hive%20like%20%E8%AF%AD%E6%B3%95<\/a><\/p>\n\n\n\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u4ee5\u524d\u77e5\u9053SQL\u4e2d\u7684 like \u548c rlike \u662f\u6709\u533a\u522b\u7684\uff0c\u5dee\u522b\u4e3b\u8981\u5728\u4e8e\u524d\u8005\u53ea\u652f\u6301 \u767e\u5206 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,23,7],"tags":[491,1261,483,2039,2040,484,1262],"class_list":["post-5530","post","type-post","status-publish","format-standard","hentry","category-database","category-knowledgebase-2","category-programing","tag-concat","tag-hive","tag-like","tag-regexp","tag-regexp_replace","tag-rlike","tag-sql"],"views":1875,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5530","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=5530"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5530\/revisions"}],"predecessor-version":[{"id":5531,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5530\/revisions\/5531"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=5530"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=5530"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=5530"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}