{"id":556560,"date":"2022-03-22T22:11:17","date_gmt":"2022-03-22T14:11:17","guid":{"rendered":"http:\/\/4563.org\/?p=556560"},"modified":"2022-03-22T22:11:17","modified_gmt":"2022-03-22T14:11:17","slug":"%e8%af%b7%e6%95%99%e4%b8%80%e4%b8%aa-sql-%e5%86%99%e6%b3%95","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=556560","title":{"rendered":"\u8bf7\u6559\u4e00\u4e2a SQL \u5199\u6cd5"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1> \u8bf7\u6559\u4e00\u4e2a SQL \u5199\u6cd5 <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : imherer <\/span>  <span><i><\/i> 41<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u6570\u636e\u5e93\u662f Postgresql \u5047\u5982\u6709\u5982\u4e0b\u8868\u6570\u636e<\/p>\n<table>\n<thead>\n<tr>\n<th>id<\/th>\n<th>name<\/th>\n<th>dep<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>1<\/td>\n<td>[1,2,3]<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2<\/td>\n<td>[2]<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>3<\/td>\n<td>[3]<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6211\u60f3\u5f97\u5230\u7684\u7c7b\u4f3c\u4e0b\u9762\u7684 SQL \u7ed3\u679c<\/p>\n<p><code>SELECT * FROM table WHERE dep=2<\/code><\/p>\n<table>\n<thead>\n<tr>\n<th>id<\/th>\n<th>name<\/th>\n<th>dep<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>1<\/td>\n<td>[1,2,3]<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2<\/td>\n<td>[2]<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><code>SELECT * FROM table WHERE dep=3<\/code><\/p>\n<table>\n<thead>\n<tr>\n<th>id<\/th>\n<th>name<\/th>\n<th>dep<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>1<\/td>\n<td>[1,2,3]<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>3<\/td>\n<td>[3]<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u53ea\u8981 dep \u5217\u5305\u542b\u7ed9\u5b9a\u7684\u503c\u5c31\u6ee1\u8db3\u6761\u4ef6\uff0c\u7ed9\u5b9a\u7684\u503c\u6c38\u8fdc\u53ea\u53ef\u80fd\u662f\u4e00\u4e2a\u6570\u5b57\uff0c\u4e0d\u53ef\u80fd\u662f\u6570\u7ec4<\/p>\n<p>\u6211\u73b0\u5728\u7684\u505a\u6cd5\u662f dep \u5217\u7528 jsonb \u5b58\u50a8\u7684\uff0c\u7136\u540e\u7528 jsonb \u7684\u8bed\u6cd5\u5b9e\u73b0\u4e86\uff0c\u6709\u6ca1\u6709\u4ec0\u4e48\u66f4\u597d\u7684\u65b9\u6848\u5462<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>9<\/span>) <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"7155289\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : PrtScScrLk <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u6a21\u7cca\u67e5\u8be2\u4e0d\u77e5\u9053\u884c\u4e0d\u884c\uff1f like %2%\u8fd9\u79cd <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7155290\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : imherer <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @PrtScScrLk \u6a21\u7cca\u67e5\u8be2\u4e0d\u884c\uff0c\u5fc5\u987b\u8981\u7cbe\u786e\u67e5\u627e\uff0c \u56e0\u4e3a\u5982\u679c\u6709\u8fd9\u6837\u7684\u4e24\u5217\u6570\u636e[1,23,4] [1,2] \u8fd9\u6837\u6a21\u7cca\u67e5\u8be2%2%\u7684\u8bdd\u5c31\u4e0d\u6b63\u786e <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7155291\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yuhangch <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u7528 array \u4e5f\u884c\uff0chttps:\/\/www.postgresql.org\/docs\/current\/functions-array.html#ARRAY-OPERATORS-TABLE <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7155292\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : imherer <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @yuhangch \u611f\u8c22\uff0c\u641e\u5b9a\u4e86\uff01 \u5176\u5b9e\u6211\u4e00\u5f00\u59cb\u5c31\u662f\u7528\u7684 array \uff0c\u4f46\u662f\u4e0d\u77e5\u9053\u600e\u4e48\u5199 sql \u624d\u6539\u7528\u7684 jsonb <br \/>\u7b11\u54ed \u54ed\u7b11 \u7b11\u51fa\u773c\u6cea \u7834\u6d95\u4e3a\u7b11 \u7b11\u6b7b \u7b11\u5c3f \u7b11 cry <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7155293\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zhazi <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> https:\/\/stackoverflow.com\/questions\/39643454\/postgres-check-if-array-field-contains-value <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7155294\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : imherer <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @zhazi \u5389\u5bb3\u4e86\uff0c\u8fd8\u6709\u8fd9\u4e2a\u5199\u6cd5\uff0c\u8bd5\u4e86\u4e0b\u5b8c\u5168\u53ef\u4ee5 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7155295\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : moen <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> SELECT * FROM table WHERE dep @&gt; ARRAY[2]<br \/>SELECT * FROM table WHERE dep @&gt; ARRAY[3]<br \/>PS\uff1a\u5efa\u8bae\u517b\u6210\u591a\u770b\u770b\u6587\u6863\u7684\u4e60\u60ef <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7155296\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : imherer <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @moen \u5927\u4f6c\u8bf4\u7684\u662f <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7155297\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : PrtScScrLk <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u597d\u5bb6\u4f19\uff0c\u5b66\u5230\u4e86\u5b66\u5230\u4e86 <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u8bf7\u6559\u4e00\u4e2a SQL \u5199\u6cd5 \u8cc7\u6df1\u5927\u4f6c &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/556560"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=556560"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/556560\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=556560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=556560"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=556560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}