{"id":632,"date":"2023-02-26T11:22:14","date_gmt":"2023-02-26T03:22:14","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=632"},"modified":"2023-04-29T20:14:42","modified_gmt":"2023-04-29T12:14:42","slug":"sql-search-for-existence-stop-use-count","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/26\/sql-search-for-existence-stop-use-count\/","title":{"rendered":"SQL\u67e5\u627e\u662f\u5426\u5b58\u5728\uff0c\u522b\u518dcount\u4e86\uff01"},"content":{"rendered":"<p>\u6839\u636e\u67d0\u4e00\u6761\u4ef6\u4ece\u6570\u636e\u5e93\u8868\u4e2d\u67e5\u8be2 \u300e\u6709\u300f\u4e0e\u300e\u6ca1\u6709\u300f\uff0c\u53ea\u6709\u4e24\u79cd\u72b6\u6001\uff0c\u90a3\u4e3a\u4ec0\u4e48\u5728\u5199SQL\u7684\u65f6\u5019\uff0c\u8fd8\u8981<code>SELECT count(*)<\/code>\u5462\uff1f<\/p>\n<h2>\u76ee\u524d\u591a\u6570\u4eba\u7684\u5199\u6cd5<\/h2>\n<p>\u591a\u6b21REVIEW\u4ee3\u7801\u65f6\uff0c\u53d1\u73b0\u5982\u73b0\u73b0\u8c61\uff1a<\/p>\n<p><!-- more --><\/p>\n<p>\u4e1a\u52a1\u4ee3\u7801\u4e2d\uff0c\u9700\u8981\u6839\u636e\u4e00\u4e2a\u6216\u591a\u4e2a\u6761\u4ef6\uff0c\u67e5\u8be2\u662f\u5426\u5b58\u5728\u8bb0\u5f55\uff0c\u4e0d\u5173\u5fc3\u6709\u591a\u5c11\u6761\u8bb0\u5f55\u3002\u666e\u904d\u7684SQL\u53ca\u4ee3\u7801\u5199\u6cd5\u5982\u4e0b<\/p>\n<pre><code class=\"language-java\">##### SQL\u5199\u6cd5:\nSELECT count(*) FROM table WHERE a = 1 AND b = 2\n\n##### Java\u5199\u6cd5:\nint nums = xxDao.countXxxxByXxx(params);\nif ( nums &gt; 0 ) {\n  \/\/\u5f53\u5b58\u5728\u65f6\uff0c\u6267\u884c\u8fd9\u91cc\u7684\u4ee3\u7801\n} else {\n  \/\/\u5f53\u4e0d\u5b58\u5728\u65f6\uff0c\u6267\u884c\u8fd9\u91cc\u7684\u4ee3\u7801\n}<\/code><\/pre>\n<h2>\u4f18\u5316\u65b9\u6848<\/h2>\n<p>\u63a8\u8350\u5199\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">##### SQL\u5199\u6cd5:\nSELECT 1 FROM table WHERE a = 1 AND b = 2 LIMIT 1\n\n##### Java\u5199\u6cd5:\nInteger exist = xxDao.existXxxxByXxx(params);\nif ( exist != NULL ) {\n  \/\/\u5f53\u5b58\u5728\u65f6\uff0c\u6267\u884c\u8fd9\u91cc\u7684\u4ee3\u7801\n} else {\n  \/\/\u5f53\u4e0d\u5b58\u5728\u65f6\uff0c\u6267\u884c\u8fd9\u91cc\u7684\u4ee3\u7801\n}<\/code><\/pre>\n<p>SQL\u4e0d\u518d\u4f7f\u7528<code>count<\/code>\uff0c\u800c\u662f\u6539\u7528<code>LIMIT 1<\/code>\uff0c\u8ba9\u6570\u636e\u5e93\u67e5\u8be2\u65f6\u9047\u5230\u4e00\u6761\u5c31\u8fd4\u56de\uff0c\u4e0d\u8981\u518d\u7ee7\u7eed\u67e5\u627e\u8fd8\u6709\u591a\u5c11\u6761\u4e86<\/p>\n<p>\u4e1a\u52a1\u4ee3\u7801\u4e2d\u76f4\u63a5\u5224\u65ad\u662f\u5426\u975e\u7a7a\u5373\u53ef<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u6839\u636e\u67e5\u8be2\u6761\u4ef6\u67e5\u51fa\u6765\u7684\u6761\u6570\u8d8a\u591a\uff0c\u6027\u80fd\u63d0\u5347\u7684\u8d8a\u660e\u663e\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u8fd8\u53ef\u4ee5\u51cf\u5c11\u8054\u5408\u7d22\u5f15\u7684\u521b\u5efa\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6839\u636e\u67d0\u4e00\u6761\u4ef6\u4ece\u6570\u636e\u5e93\u8868\u4e2d\u67e5\u8be2 \u300e\u6709\u300f\u4e0e\u300e\u6ca1\u6709\u300f\uff0c\u53ea\u6709\u4e24\u79cd\u72b6\u6001\uff0c\u90a3\u4e3a\u4ec0\u4e48\u5728\u5199SQL\u7684\u65f6\u5019\uff0c\u8fd8\u8981SELECT co [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-632","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/632","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=632"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/632\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=632"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=632"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}