{"id":45970,"date":"2024-12-01T12:35:45","date_gmt":"2024-12-01T04:35:45","guid":{"rendered":"https:\/\/fwq.ai\/blog\/45970\/"},"modified":"2024-12-01T12:35:45","modified_gmt":"2024-12-01T04:35:45","slug":"sequelize-%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e5%a4%8d%e6%9d%82%e7%9a%84%e7%bb%84%e5%90%88%e6%9f%a5%e8%af%a2%ef%bc%9f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/45970\/","title":{"rendered":"Sequelize \u5982\u4f55\u5b9e\u73b0\u590d\u6742\u7684\u7ec4\u5408\u67e5\u8be2\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Sequelize \u5982\u4f55\u5b9e\u73b0\u590d\u6742\u7684\u7ec4\u5408\u67e5\u8be2\uff1f<\/h1>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u6211\u4eec\u8981\u52aa\u529b\u5b66\u4e60\u5566\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4e0b\u6587\u4e2d\u7684\u5185\u5bb9\u6211\u4eec\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5728\u9605\u8bfb\u672c\u6587\u8fc7\u7a0b\u4e2d\u6709\u9047\u5230\u4e0d\u6e05\u695a\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u7559\u8a00\u5440\uff01\u6211\u4eec\u4e00\u8d77\u8ba8\u8bba\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241121\/1732189910673f1ed6586bd.jpg\" class=\"aligncenter\" title=\"Sequelize \u5982\u4f55\u5b9e\u73b0\u590d\u6742\u7684\u7ec4\u5408\u67e5\u8be2\uff1f\u63d2\u56fe\" alt=\"Sequelize \u5982\u4f55\u5b9e\u73b0\u590d\u6742\u7684\u7ec4\u5408\u67e5\u8be2\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 sequelize \u4e2d\u5904\u7406\u590d\u6742\u7684\u7ec4\u5408\u67e5\u8be2\uff1f<\/strong><\/p>\n<p>\u5728 sequelize \u4e2d\u8fdb\u884c\u7ec4\u5408\u67e5\u8be2\u65f6\uff0c\u4f7f\u7528 where \u5b50\u53e5\u5e76\u7ed3\u5408 op \u64cd\u4f5c\u7b26\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u3002<\/p>\n<p>\u4e3a\u4e86\u751f\u6210\u590d\u6742\u7684\u67e5\u8be2\u6761\u4ef6\uff0c\u4f60\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u64cd\u4f5c\uff1a<\/p>\n<ol>\n<li>\n<p><strong>\u751f\u6210\u67e5\u8be2\u5b57\u7b26\u4e32\uff1a<\/strong>\u6839\u636e\u4f20\u5165\u7684\u53c2\u6570\uff08 \u043f\u043e\u043b\u0435\u3001\u503c\u3001\u64cd\u4f5c\u7b26\uff09\uff0c\u4f7f\u7528 op \u64cd\u4f5c\u7b26\u751f\u6210 sql \u67e5\u8be2\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>if (_item[0] == 'and') {\n if (_item[3] == 'equals') {\n     _where_arr[_item[1]] = _item[2]\n } else if (_item[3] == 'like') {\n     _where_arr[_item[1]] = {\n         [_this.sequelize_op.like]: '%' + _item[2] + '%'\n     }\n }\n}<\/pre>\n<\/li>\n<li>\n<p><strong>\u6784\u5efa where \u5bf9\u8c61\uff1a<\/strong>\u5c06\u751f\u6210\u7684\u67e5\u8be2\u6761\u4ef6\u6dfb\u52a0\u5230 where \u5bf9\u8c61\u4e2d\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>where = {'\u5b57\u6bb5c' : \"xxxx\" , [op.and] : literal , '\u5b57\u6bb5b':{ [op.like] : '%xxx%'} }<\/pre>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528 findone \u6216\u5176\u4ed6\u67e5\u8be2\u65b9\u6cd5\uff0cwhere \u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\uff1a<\/strong><\/p>\n<pre>let item = await Test.findOne(params);<\/pre>\n<\/li>\n<\/ol>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li>\u8981\u6ce8\u610f\u5904\u7406 and \u548c or \u64cd\u4f5c\u7b26\u3002<\/li>\n<li>\u6839\u636e\u5b9e\u9645\u9700\u6c42\u8c03\u6574\u751f\u6210 where \u5bf9\u8c61\u7684\u65b9\u5f0f\u3002<\/li>\n<li>\u4f7f\u7528 sequelize \u7684 op \u64cd\u4f5c\u7b26\uff0c\u53ef\u5927\u5927\u7b80\u5316\u590d\u6742\u67e5\u8be2\u7684\u7f16\u5199\u3002<\/li>\n<\/ul>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aSequelize \u5982\u4f55\u5b9e\u73b0\u590d\u6742\u7684\u7ec4\u5408\u67e5\u8be2\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u5427\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Sequelize \u5982\u4f55\u5b9e\u73b0\u590d\u6742\u7684\u7ec4\u5408\u67e5\u8be2\uff1f \u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u6211\u4eec\u8981\u52aa\u529b\u5b66\u4e60\u5566\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4e0b\u6587\u4e2d\u7684\u5185\u5bb9\u6211\u4eec\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5728\u9605\u8bfb\u672c\u6587\u8fc7\u7a0b\u4e2d\u6709\u9047\u5230\u4e0d\u6e05\u695a\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u7559\u8a00\u5440\uff01\u6211\u4eec\u4e00\u8d77\u8ba8\u8bba\uff0c\u4e00\u8d77\u5b66\u4e60\uff01 \u5982\u4f55\u5728 sequelize \u4e2d\u5904\u7406\u590d\u6742\u7684\u7ec4\u5408\u67e5\u8be2\uff1f \u5728 sequelize \u4e2d\u8fdb\u884c\u7ec4\u5408\u67e5\u8be2\u65f6\uff0c\u4f7f\u7528 where \u5b50\u53e5\u5e76\u7ed3\u5408 op \u64cd\u4f5c\u7b26\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u3002 \u4e3a\u4e86\u751f\u6210\u590d\u6742\u7684\u67e5\u8be2\u6761\u4ef6\uff0c\u4f60\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u64cd\u4f5c\uff1a \u751f\u6210\u67e5\u8be2\u5b57\u7b26\u4e32\uff1a\u6839\u636e\u4f20\u5165\u7684\u53c2\u6570\uff08 \u043f\u043e\u043b\u0435\u3001\u503c\u3001\u64cd\u4f5c\u7b26\uff09\uff0c\u4f7f\u7528 op \u64cd\u4f5c\u7b26\u751f\u6210 sql \u67e5\u8be2\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a if (_item[0] == &#8216;and&#8217;) { if (_item[3] == &#8216;equals&#8217;) { _where_arr[_item[1]] = _item[2] } else if (_item[3] == &#8216;like&#8217;) { _where_arr[_item[1]] = { [_this.sequelize_op.like]: &#8216;%&#8217; + _item[2] + &#8216;%&#8217; } } } \u6784\u5efa where \u5bf9\u8c61\uff1a\u5c06\u751f\u6210\u7684\u67e5\u8be2\u6761\u4ef6\u6dfb\u52a0\u5230 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-45970","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/45970","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=45970"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/45970\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=45970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=45970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=45970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}