{"id":453,"date":"2023-02-04T08:17:10","date_gmt":"2023-02-04T00:17:10","guid":{"rendered":"http:\/\/blog.yuekegu.com\/?p=453"},"modified":"2023-02-04T08:17:10","modified_gmt":"2023-02-04T00:17:10","slug":"yii2-where%e6%9f%a5%e8%af%a2%e6%9d%a1%e4%bb%b6%e6%95%b4%e7%90%86","status":"publish","type":"post","link":"https:\/\/book.yuekegu.com\/index.php\/2023\/02\/04\/yii2-where%e6%9f%a5%e8%af%a2%e6%9d%a1%e4%bb%b6%e6%95%b4%e7%90%86\/","title":{"rendered":"YII2 where\u67e5\u8be2\u6761\u4ef6\u6574\u7406"},"content":{"rendered":"\n<p>\u672c\u6587\u8f6c\u81ea\uff1ahttps:\/\/www.yiichina.com\/tutorial\/1405<\/p>\n\n\n\n<p>\u5b98\u65b9\u6587\u6863\uff1a<a href=\"http:\/\/www.yiichina.com\/doc\/guide\/2.0\/db-query-builder\">http:\/\/www.yiichina.com\/doc\/guide\/2.0\/db-query-builder<\/a><\/p>\n\n\n\n<p>\u6587\u7ae0\u6d89\u53cawhere\u3001 addParams \u3001filterWhere \u3001andWhere\u3001orWhere\u3001 andFilterWhere()\u3001 orFilterWhere()\u3001andFilterCompare()<br>\u4f46\u662f\u683c\u5f0f\u662f\u4e00\u6837\u7684<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5b57\u7b26\u4e32\u683c\u5f0f\uff0c\u4f8b\u5982\uff1a'status=1'\n\u54c8\u5e0c\u683c\u5f0f\uff0c\u4f8b\u5982\uff1a ['status' => 1, 'type' => 2]\n\u64cd\u4f5c\u7b26\u683c\u5f0f\uff0c\u4f8b\u5982\uff1a['like', 'name', 'test']\n<\/code><\/pre>\n\n\n\n<p>\u5b57\u7b26\u4e32\u548c\u54c8\u5e0c\u683c\u5f0f\u5f88\u597d\u7406\u89e3\uff0c\u6211\u4eec\u6765\u770b\u770b\u64cd\u4f5c\u7b26\u683c\u5f0f\uff0c\u56e0\u4e3a\u64cd\u4f5c\u7b26\u683c\u5f0f\u53ef\u4ee5\u7ec4\u6210\u76f8\u5bf9\u590d\u6742\u7684\u67e5\u8be2\u8bed\u53e5<br>\u6700\u7b80\u5355\u7684\u5c31\u662f\u5b98\u65b9\u7ed9\u7684\u4f8b\u5b50<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$status = 10;\n$search = 'yii';\n\n$query->where(['status' => $status]);\n\nif (!empty($search)) {\n    $query->andWhere(['like', 'title', $search]);\n}\n\u751f\u6210\u7684\u8bed\u53e5\u5c31\u662f\n... WHERE (`status` = 10) AND (`title` LIKE '\n<\/code><\/pre>\n\n\n\n<p>\u64cd\u4f5c\u7b26\u683c\u5f0f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>[\u64cd\u4f5c\u7b26, \u64cd\u4f5c\u65701, \u64cd\u4f5c\u65702, ...]\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.yiichina.com\/uploads\/images\/201707\/25163556654.png\"><img decoding=\"async\" src=\"https:\/\/www.yiichina.com\/uploads\/images\/201707\/25163556654_thumb.png\" alt=\"0cb4d39e-418f-4183-a900-8a4ab5b1aadf.png\"\/><\/a><\/figure>\n\n\n\n<p>\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u64cd\u4f5c\u7b26<br>\u64cd\u4f5c\u7b26\u5305\u62ecand\u3001or\u3001 like\u3001in\u3001 between\u7b49<br>\u7b2c\u4e8c\u4e2a\u7b2c\u4e09\u4e2a\u90fd\u662f\u64cd\u4f5c\u6570<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u7b2c\u4e00\u79cd\u6700\u7b80\u5355\u7684\u5c31\u662f\u4e0a\u9762\u63d0\u5230\u7684\u4f8b\u5b50\n andWhere(['like', 'title','\u641c\u7d22\u7684\u6807\u9898']);\n\u751f\u6210\u7684\u8bed\u53e5\n... WHERE (`status` = 10) AND (`title` LIKE '\n\u7b2c\u4e8c\u79cd\naddWhere(['and', 'id=1', 'name=2']);\n\u751f\u6210\u7684\u8bed\u53e5\n... WHERE id=1 AND name=2\n\u7b2c\u4e09\u79cd\naddWhere(['and', 'type=1', ['or', 'id=1', 'id=2']]);\n\u751f\u6210\u7684\u8bed\u53e5\n... WHERE type=1 AND (id=1 OR id=2);\n\u7b2c\u56db\u79cd\n->andWhere(['or like','name',['\u54c8\u54c8','\u82e6\u82e6']]);\n\u751f\u6210\u7684\u8bed\u53e5\n WHERE `name` LIKE '\n \u7b2c\u4e94\u79cd\n addWhere(['or',['like','name','\u54c8\u54c8'],['like','title','\u82e6\u82e6']]);\/\/\u64cd\u4f5c\u7b26\u683c\u5f0f\u7684\u5d4c\u5957\n \u751f\u6210\u7684\u8bed\u53e5\n... WHERE (`status`=1) AND ((`name` LIKE '\n\n\n\n<p>\n\n$query-&gt;andWhere(new Expression(&#8216;FIND_IN_SET(&#8216;1,size&#8217;)&#8217;));+<br>\u751f\u6210\u7684\u8bed\u53e5<br>&#8230; WHERE id=1 AND FIND_IN_SET(1, size)<br>\/\/size\u7684\u503c\uff1a1,2,3,4,5\n\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u8f6c\u81ea\uff1ahttps:\/\/www.yiichina.com\/tutorial\/1405 \u5b98\u65b9\u6587\u6863\uff1ahttp:\/\/www.yiichina.com\/doc\/guide\/2.0\/db-query-builder \u6587\u7ae0\u6d89\u53cawhere\u3001 addParams \u3001filterWhere \u3001andWhere\u3001orWhere\u3001 andFilterWhere()\u3001 orFilterWhere()\u3001andFilterCompare()\u4f46\u662f\u683c\u5f0f\u662f\u4e00\u6837\u7684 \u5b57\u7b26\u4e32\u548c\u54c8\u5e0c\u683c\u5f0f\u5f88\u597d\u7406\u89e3\uff0c\u6211\u4eec\u6765\u770b\u770b\u64cd\u4f5c\u7b26\u683c\u5f0f\uff0c\u56e0\u4e3a\u64cd\u4f5c\u7b26\u683c\u5f0f\u53ef\u4ee5\u7ec4\u6210\u76f8\u5bf9\u590d\u6742\u7684\u67e5\u8be2\u8bed\u53e5\u6700\u7b80\u5355\u7684\u5c31\u662f\u5b98\u65b9\u7ed9\u7684\u4f8b\u5b50 \u64cd\u4f5c\u7b26\u683c\u5f0f \u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u64cd\u4f5c\u7b26\u64cd\u4f5c\u7b26\u5305\u62ecand\u3001or\u3001 like\u3001in\u3001 between\u7b49\u7b2c\u4e8c\u4e2a\u7b2c\u4e09\u4e2a\u90fd\u662f\u64cd\u4f5c\u6570 $query-&gt;andWhere(new Expression(&#8216;FIND_IN_SET(&#8216;1,size&#8217;)&#8217;));+\u751f\u6210\u7684\u8bed\u53e5&#8230; WHERE id=1 AND FIND_IN_SET(1, size)\/\/size\u7684\u503c\uff1a1,2,3,4,5<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-453","post","type-post","status-publish","format-standard","hentry","category-yii2"],"_links":{"self":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts\/453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/comments?post=453"}],"version-history":[{"count":0,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts\/453\/revisions"}],"wp:attachment":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/media?parent=453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/categories?post=453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/tags?post=453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}