{"id":23155,"date":"2024-11-21T11:49:21","date_gmt":"2024-11-21T03:49:21","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23155\/"},"modified":"2024-11-21T11:49:21","modified_gmt":"2024-11-21T03:49:21","slug":"thinkphp5-0%e5%af%b9%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e6%93%8d%e4%bd%9c%e6%96%b9%e6%b3%95%e6%9c%89%e5%93%aa%e4%ba%9b","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23155\/","title":{"rendered":"Thinkphp5.0\u5bf9\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u65b9\u6cd5\u6709\u54ea\u4e9b"},"content":{"rendered":"<h3><strong>\u57fa\u672c\u4f7f\u7528<\/strong><\/h3>\n<p><strong>\u67e5\u8be2\u64cd\u4f5c<\/strong><\/p>\n<pre>Db::query('select&nbsp;*&nbsp;from&nbsp;think_user&nbsp;where&nbsp;id=?',[8]);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5199\u5165\u64cd\u4f5c<\/strong><\/p>\n<pre>Db::execute('insert&nbsp;into&nbsp;think_user&nbsp;(id,&nbsp;name)&nbsp;values&nbsp;(?,&nbsp;?)',[8,'thinkphp']);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3><strong>\u67e5\u8be2\u6784\u9020\u5668<\/strong><\/h3>\n<p><strong>\u67e5\u8be2\u6570\u636e<\/strong><\/p>\n<p>find \u8fd4\u56de\u4e00\u6761\u8bb0\u5f55\uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4 \u5982\u679c\u7ed3\u679c\u4e0d\u5b58\u5728\uff0c\u8fd4\u56deNULL<\/p>\n<pre>Db::table('think_user')-&amp;gt;where('id',1)-&amp;gt;find();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>select \u8fd4\u56de\u6240\u6709\u8bb0\u5f55\uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4 \u5982\u679c\u7ed3\u679c\u4e0d\u5b58\u5728\uff0c\u8fd4\u56de\u4e00\u4e2a\u7a7a\u6570\u7ec4<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>Db::table('think_user')-&amp;gt;where('status',1)-&amp;gt;select();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h4><strong>\u52a9\u624b\u51fd\u6570<\/strong><\/h4>\n<p>\u7cfb\u7edf\u63d0\u4f9b\u4e86\u4e00\u4e2adb\u52a9\u624b\u51fd\u6570\uff0c\u53ef\u4ee5\u66f4\u65b9\u4fbf\u7684\u67e5\u8be2\uff1a<\/p>\n<pre>db('user')-&amp;gt;where('id',1)-&amp;gt;find();\ndb('user')-&amp;gt;where('status',1)-&amp;gt;select();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\uff1a\u4f7f\u7528db\u52a9\u624b\u51fd\u6570\u9ed8\u8ba4\u6bcf\u6b21\u90fd\u4f1a\u91cd\u65b0\u8fde\u63a5\u6570\u636e\u5e93\uff0c\u800c\u4f7f\u7528Db::name\u6216\u8005Db::table\u65b9\u6cd5\u7684\u8bdd\u90fd\u662f\u5355\u4f8b\u7684\u3002db\u51fd\u6570\u5982\u679c\u9700\u8981\u91c7\u7528\u76f8\u540c\u7684\u94fe\u63a5\uff0c\u53ef\u4ee5\u4f20\u5165\u7b2c\u4e09\u4e2a\u53c2\u6570\uff0c\u4f8b\u5982<\/strong><\/p>\n<pre>db('user',[],false)-&amp;gt;where('id',1)-&amp;gt;find();\ndb('user',[],false)-&amp;gt;where('status',1)-&amp;gt;select();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6dfb\u52a0\u6570\u636e<\/strong><\/p>\n<p>\u4f7f\u7528 Db \u7c7b\u7684 insert \u65b9\u6cd5\u5411\u6570\u636e\u5e93\u63d0\u4ea4\u6570\u636e<\/p>\n<pre>$data&nbsp;=&nbsp;['foo'&nbsp;=&amp;gt;&nbsp;'bar',&nbsp;'bar'&nbsp;=&amp;gt;&nbsp;'foo'];\nDb::table('think_user')-&amp;gt;insert($data);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6dfb\u52a0\u591a\u6761\u6570\u636e<\/strong><\/p>\n<p>\u6dfb\u52a0\u591a\u6761\u6570\u636e\u76f4\u63a5\u5411 Db \u7c7b\u7684 insertAll \u65b9\u6cd5\u4f20\u5165\u9700\u8981\u6dfb\u52a0\u7684\u6570\u636e\u5373\u53ef<\/p>\n<pre>$data&nbsp;=&nbsp;[&nbsp;&nbsp;&nbsp;&nbsp;['foo'&nbsp;=&amp;gt;&nbsp;'bar',&nbsp;'bar'&nbsp;=&amp;gt;&nbsp;'foo'],\n&nbsp;&nbsp;&nbsp;&nbsp;['foo'&nbsp;=&amp;gt;&nbsp;'bar1',&nbsp;'bar'&nbsp;=&amp;gt;&nbsp;'foo1'],\n&nbsp;&nbsp;&nbsp;&nbsp;['foo'&nbsp;=&amp;gt;&nbsp;'bar2',&nbsp;'bar'&nbsp;=&amp;gt;&nbsp;'foo2']\n];\nDb::name('user')-&amp;gt;insertAll($data);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>insertAll \u65b9\u6cd5\u6dfb\u52a0\u6570\u636e\u6210\u529f\u8fd4\u56de\u6dfb\u52a0\u6210\u529f\u7684\u6761\u6570<\/p>\n<p><strong>\u52a9\u624b\u51fd\u6570<\/strong><\/p>\n<pre>\/\/&nbsp;\u6dfb\u52a0\u5355\u6761\u6570\u636e\ndb('user')-&amp;gt;insert($data);\n\n\/\/&nbsp;\u6dfb\u52a0\u591a\u6761\u6570\u636e\ndb('user')-&amp;gt;insertAll($list);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3><strong>\u66f4\u65b0\u6570\u636e<\/strong><\/h3>\n<p>update \u65b9\u6cd5\u8fd4\u56de\u5f71\u54cd\u6570\u636e\u7684\u6761\u6570\uff0c\u6ca1\u4fee\u6539\u4efb\u4f55\u6570\u636e\u8fd4\u56de 0<\/p>\n<pre>Db::table('think_user')-&amp;gt;where('id',&nbsp;1)-&amp;gt;update(['name'&nbsp;=&amp;gt;&nbsp;'thinkphp']);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u66f4\u65b0\u67d0\u4e2a\u5b57\u6bb5\u7684\u503c\uff1a setField \u65b9\u6cd5\u8fd4\u56de\u5f71\u54cd\u6570\u636e\u7684\u6761\u6570\uff0c\u6ca1\u4fee\u6539\u4efb\u4f55\u6570\u636e\u5b57\u6bb5\u8fd4\u56de 0<\/p>\n<pre>Db::table('think_user')-&amp;gt;where('id',1)-&amp;gt;setField('name',&nbsp;'thinkphp');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<pre>\u81ea\u589e\u6216\u81ea\u51cf\u4e00\u4e2a\u5b57\u6bb5\u7684\u503c\n\/\/&nbsp;score&nbsp;\u5b57\u6bb5\u52a0&nbsp;1\nDb::table('think_user')-&amp;gt;where('id',&nbsp;1)-&amp;gt;setInc('score');\n\/\/&nbsp;score&nbsp;\u5b57\u6bb5\u52a0&nbsp;5\nDb::table('think_user')-&amp;gt;where('id',&nbsp;1)-&amp;gt;setInc('score',&nbsp;5);\n\/\/&nbsp;score&nbsp;\u5b57\u6bb5\u51cf&nbsp;1\nDb::table('think_user')-&amp;gt;where('id',&nbsp;1)-&amp;gt;setDec('score');\n\/\/&nbsp;score&nbsp;\u5b57\u6bb5\u51cf&nbsp;5\nDb::table('think_user')-&amp;gt;where('id',&nbsp;1)-&amp;gt;setDec('score',&nbsp;5);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5ef6\u8fdf\u66f4\u65b0<\/p>\n<pre>Db::table('think_user')-&amp;gt;where('id',&nbsp;1)-&amp;gt;setInc('score',&nbsp;1,&nbsp;10);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3><strong>\u5220\u9664\u6570\u636e<\/strong><\/h3>\n<pre>\/\/&nbsp;\u6839\u636e\u4e3b\u952e\u5220\u9664\nDb::table('think_user')-&amp;gt;delete(1);\nDb::table('think_user')-&amp;gt;delete([1,2,3]);\n\n\/\/&nbsp;\u6761\u4ef6\u5220\u9664&nbsp;&nbsp;&nbsp;&nbsp;\nDb::table('think_user')-&amp;gt;where('id',1)-&amp;gt;delete();\nDb::table('think_user')-&amp;gt;where('id','delete();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3><strong>\u6761\u4ef6\u67e5\u8be2\u65b9\u6cd5<\/strong><\/h3>\n<p><strong>where<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528where\u65b9\u6cd5\u8fdb\u884cAND\u6761\u4ef6\u67e5\u8be2\uff1a<\/p>\n<pre>Db::table('think_user')\n&nbsp;&nbsp;&nbsp;&nbsp;-&amp;gt;where('name','like','%thinkphp')\n&nbsp;&nbsp;&nbsp;&nbsp;-&amp;gt;where('status',1)\n&nbsp;&nbsp;&nbsp;&nbsp;-&amp;gt;find();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>whereOr\u65b9\u6cd5<\/strong><\/p>\n<pre>Db::table('think_user')\n&nbsp;&nbsp;&nbsp;&nbsp;-&amp;gt;where('name','like','%thinkphp')\n&nbsp;&nbsp;&nbsp;&nbsp;-&amp;gt;whereOr('title','like','%thinkphp')\n&nbsp;&nbsp;&nbsp;&nbsp;-&amp;gt;find();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6df7\u5408\u67e5\u8be2<\/strong><\/p>\n<p>where\u65b9\u6cd5\u548cwhereOr\u65b9\u6cd5\u5728\u590d\u6742\u7684\u67e5\u8be2\u6761\u4ef6\u4e2d\u7ecf\u5e38\u9700\u8981\u914d\u5408\u4e00\u8d77\u6df7\u5408\u4f7f\u7528\uff0c\u4e0b\u9762\u4e3e\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre>$result&nbsp;=&nbsp;Db::table('think_user')-&amp;gt;where(function&nbsp;($query)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;$query-&amp;gt;where('id',&nbsp;1)-&amp;gt;whereor('id',&nbsp;2);\n})-&amp;gt;whereOr(function&nbsp;($query)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;$query-&amp;gt;where('name',&nbsp;'like',&nbsp;'think')-&amp;gt;whereOr('name',&nbsp;'like',&nbsp;'thinkphp');\n})-&amp;gt;select();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3><strong>\u67e5\u8be2\u8868\u8fbe\u5f0f<\/strong><\/h3>\n<p>\u67e5\u8be2\u8868\u8fbe\u5f0f\u652f\u6301\u5927\u90e8\u5206\u7684SQL\u67e5\u8be2\u8bed\u6cd5\uff0c\u4e5f\u662fThinkPHP\u67e5\u8be2\u8bed\u8a00\u7684\u7cbe\u9ad3\uff0c\u67e5\u8be2\u8868\u8fbe\u5f0f\u7684\u4f7f\u7528\u683c\u5f0f\uff1a<\/p>\n<pre>where('\u5b57\u6bb5\u540d','\u8868\u8fbe\u5f0f','\u67e5\u8be2\u6761\u4ef6');\nwhereOr('\u5b57\u6bb5\u540d','\u8868\u8fbe\u5f0f','\u67e5\u8be2\u6761\u4ef6');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<pre>\u8868\u8fbe\u5f0f&nbsp;\u542b\u4e49\nEQ\u3001=&nbsp;\u7b49\u4e8e\uff08=\uff09\nNEQ\u3001&nbsp;\u4e0d\u7b49\u4e8e\uff08\uff09\nGT\u3001&amp;gt;&nbsp;\u5927\u4e8e\uff08&amp;gt;\uff09\nEGT\u3001&amp;gt;=&nbsp;\u5927\u4e8e\u7b49\u4e8e\uff08&amp;gt;=\uff09\nLT\u3001&nbsp;time&nbsp;\u65f6\u95f4\u6bd4\u8f83\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fThinkphp5.0\u5bf9\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u65b9\u6cd5\u6709\u54ea\u4e9b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u57fa\u672c\u4f7f\u7528 \u67e5\u8be2\u64cd\u4f5c Db::query(&#8216;select&nbsp;*&nbsp;from&nbsp;think_user&nbsp;where&nbsp;id=?&#8217;,[8]); \u767b\u5f55\u540e\u590d\u5236 \u5199\u5165\u64cd\u4f5c Db::execute(&#8216;insert&nbsp;into&nbsp;think_user&nbsp;(id,&nbsp;name)&nbsp;values&nbsp;(?,&nbsp;?)&#8217;,[8,&#8217;thinkphp&#8217;]); \u767b\u5f55\u540e\u590d\u5236 \u67e5\u8be2\u6784\u9020\u5668 \u67e5\u8be2\u6570\u636e find \u8fd4\u56de\u4e00\u6761\u8bb0\u5f55\uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4 \u5982\u679c\u7ed3\u679c\u4e0d\u5b58\u5728\uff0c\u8fd4\u56deNULL Db::table(&#8216;think_user&#8217;)-&amp;gt;where(&#8216;id&#8217;,1)-&amp;gt;find(); \u767b\u5f55\u540e\u590d\u5236 select \u8fd4\u56de\u6240\u6709\u8bb0\u5f55\uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4 \u5982\u679c\u7ed3\u679c\u4e0d\u5b58\u5728\uff0c\u8fd4\u56de\u4e00\u4e2a\u7a7a\u6570\u7ec4 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b Db::table(&#8216;think_user&#8217;)-&amp;gt;where(&#8216;status&#8217;,1)-&amp;gt;select(); \u767b\u5f55\u540e\u590d\u5236 \u52a9\u624b\u51fd\u6570 \u7cfb\u7edf\u63d0\u4f9b\u4e86\u4e00\u4e2adb\u52a9\u624b\u51fd\u6570\uff0c\u53ef\u4ee5\u66f4\u65b9\u4fbf\u7684\u67e5\u8be2\uff1a db(&#8216;user&#8217;)-&amp;gt;where(&#8216;id&#8217;,1)-&amp;gt;find(); db(&#8216;user&#8217;)-&amp;gt;where(&#8216;status&#8217;,1)-&amp;gt;select(); \u767b\u5f55\u540e\u590d\u5236 \u6ce8\u610f\uff1a\u4f7f\u7528db\u52a9\u624b\u51fd\u6570\u9ed8\u8ba4\u6bcf\u6b21\u90fd\u4f1a\u91cd\u65b0\u8fde\u63a5\u6570\u636e\u5e93\uff0c\u800c\u4f7f\u7528Db::name\u6216\u8005Db::table\u65b9\u6cd5\u7684\u8bdd\u90fd\u662f\u5355\u4f8b\u7684\u3002db\u51fd\u6570\u5982\u679c\u9700\u8981\u91c7\u7528\u76f8\u540c\u7684\u94fe\u63a5\uff0c\u53ef\u4ee5\u4f20\u5165\u7b2c\u4e09\u4e2a\u53c2\u6570\uff0c\u4f8b\u5982 db(&#8216;user&#8217;,[],false)-&amp;gt;where(&#8216;id&#8217;,1)-&amp;gt;find(); db(&#8216;user&#8217;,[],false)-&amp;gt;where(&#8216;status&#8217;,1)-&amp;gt;select(); \u767b\u5f55\u540e\u590d\u5236 \u6dfb\u52a0\u6570\u636e \u4f7f\u7528 Db \u7c7b\u7684 insert \u65b9\u6cd5\u5411\u6570\u636e\u5e93\u63d0\u4ea4\u6570\u636e $data&nbsp;=&nbsp;[&#8216;foo&#8217;&nbsp;=&amp;gt;&nbsp;&#8216;bar&#8217;,&nbsp;&#8216;bar&#8217;&nbsp;=&amp;gt;&nbsp;&#8216;foo&#8217;]; Db::table(&#8216;think_user&#8217;)-&amp;gt;insert($data); \u767b\u5f55\u540e\u590d\u5236 \u6dfb\u52a0\u591a\u6761\u6570\u636e \u6dfb\u52a0\u591a\u6761\u6570\u636e\u76f4\u63a5\u5411 Db \u7c7b\u7684 insertAll \u65b9\u6cd5\u4f20\u5165\u9700\u8981\u6dfb\u52a0\u7684\u6570\u636e\u5373\u53ef $data&nbsp;=&nbsp;[&nbsp;&nbsp;&nbsp;&nbsp;[&#8216;foo&#8217;&nbsp;=&amp;gt;&nbsp;&#8216;bar&#8217;,&nbsp;&#8216;bar&#8217;&nbsp;=&amp;gt;&nbsp;&#8216;foo&#8217;], &nbsp;&nbsp;&nbsp;&nbsp;[&#8216;foo&#8217;&nbsp;=&amp;gt;&nbsp;&#8216;bar1&#8217;,&nbsp;&#8216;bar&#8217;&nbsp;=&amp;gt;&nbsp;&#8216;foo1&#8217;], &nbsp;&nbsp;&nbsp;&nbsp;[&#8216;foo&#8217;&nbsp;=&amp;gt;&nbsp;&#8216;bar2&#8217;,&nbsp;&#8216;bar&#8217;&nbsp;=&amp;gt;&nbsp;&#8216;foo2&#8217;] ]; Db::name(&#8216;user&#8217;)-&amp;gt;insertAll($data); \u767b\u5f55\u540e\u590d\u5236 insertAll \u65b9\u6cd5\u6dfb\u52a0\u6570\u636e\u6210\u529f\u8fd4\u56de\u6dfb\u52a0\u6210\u529f\u7684\u6761\u6570 \u52a9\u624b\u51fd\u6570 \/\/&nbsp;\u6dfb\u52a0\u5355\u6761\u6570\u636e db(&#8216;user&#8217;)-&amp;gt;insert($data); [&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-23155","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23155","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=23155"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23155\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}