{"id":27817,"date":"2024-11-24T11:07:02","date_gmt":"2024-11-24T03:07:02","guid":{"rendered":"https:\/\/fwq.ai\/blog\/27817\/"},"modified":"2024-11-24T11:07:02","modified_gmt":"2024-11-24T03:07:02","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8-sql-%e7%9a%84-if-test-%e5%88%a4%e6%96%ad%e5%ad%97%e6%ae%b5%e6%98%af%e5%90%a6%e5%9c%a8%e5%88%97%e8%a1%a8%e4%b8%ad%ef%bc%9f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/27817\/","title":{"rendered":"\u5982\u4f55\u5229\u7528 SQL \u7684 if test \u5224\u65ad\u5b57\u6bb5\u662f\u5426\u5728\u5217\u8868\u4e2d\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173131996276460.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5229\u7528 SQL \u7684 if test \u5224\u65ad\u5b57\u6bb5\u662f\u5426\u5728\u5217\u8868\u4e2d\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5229\u7528 SQL \u7684 if test \u5224\u65ad\u5b57\u6bb5\u662f\u5426\u5728\u5217\u8868\u4e2d\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5229\u7528 sql \u7684 if test \u6765\u5224\u65ad\u5b57\u6bb5\u662f\u5426\u5728\u5217\u8868\u4e2d<\/strong><\/p>\n<p>\u5728\u4f7f\u7528 sql \u8bed\u53e5\u66f4\u65b0\u8bb0\u5f55\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u5224\u65ad\u67d0\u4e2a\u5b57\u6bb5\u662f\u5426\u5728\u7ed9\u5b9a\u5217\u8868\u4e2d\uff0c\u5e76\u6839\u636e\u5224\u65ad\u7ed3\u679c\u51b3\u5b9a\u662f\u5426\u66f4\u65b0\u8be5\u5b57\u6bb5\u3002\u4f20\u7edf\u7684 if-else \u8bed\u53e5\u65e0\u6cd5\u7528\u4e8e sql\uff0c\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 if test \u5b50\u53e5\u6765\u6839\u636e\u6761\u4ef6\u52a8\u6001\u751f\u6210 sql \u8bed\u53e5\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 if test \u5b50\u53e5\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>update table_name set\n  field1 = case\n    when condition1 then value1\n    else field1\n  end,\n  field2 = case\n    when condition2 then value2\n    else field2\n  end\nwhere condition3;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cif test \u5b50\u53e5\u7528\u4e8e\u68c0\u67e5 condition1 \u548c condition2 \u662f\u5426\u4e3a\u771f\u3002\u5982\u679c\u4e3a\u771f\uff0c\u5219\u5b57\u6bb5 field1 \u6216 field2 \u5c06\u66f4\u65b0\u4e3a\u76f8\u5e94\u7684\u503c value1 \u6216 value2\u3002\u5426\u5219\uff0c\u8be5\u5b57\u6bb5\u5c06\u4fdd\u6301\u5176\u539f\u59cb\u503c\u3002<\/p>\n<p>\u5728\u5177\u4f53\u573a\u666f\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 if test \u5b50\u53e5\u6765\u68c0\u67e5\u5b57\u6bb5\u662f\u5426\u5728\u7ed9\u5b9a\u5217\u8868\u4e2d\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>UPDATE school_info SET\n  schoolNo = CASE\n    WHEN 'fieldNameList' CONTAINS 'schoolNo' THEN #{entity.getSchoolNo}\n    ELSE schoolNo\n  END,\n  schoolRank = CASE\n    WHEN 'fieldNameList' CONTAINS 'schoolRank' THEN #{entity.getSchoolRank}\n    ELSE schoolRank\n  END\nWHERE dataId = #{entity.dataId};<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cif test \u5b50\u53e5\u7528\u4e8e\u68c0\u67e5 &#8216;fieldnamelist&#8217; \u5217\u8868\u662f\u5426\u5305\u542b &#8216;schoolno&#8217; \u548c &#8216;schoolrank&#8217; \u5b57\u6bb5\u3002\u5982\u679c\u5305\u542b\uff0c\u5219\u76f8\u5e94\u7684\u5b57\u6bb5\u5c06\u66f4\u65b0\u4e3a #{entity.getschoolno} \u6216 #{entity.getschoolrank}\u3002\u5426\u5219\uff0c\u8be5\u5b57\u6bb5\u5c06\u4fdd\u6301\u5176\u539f\u59cb\u503c\u3002<\/p>\n<p>\u901a\u8fc7\u4f7f\u7528 if test \u5b50\u53e5\uff0c\u4f60\u53ef\u4ee5\u52a8\u6001\u751f\u6210\u9002\u5e94\u7279\u5b9a\u6761\u4ef6\u7684 sql \u8bed\u53e5\uff0c\u4ece\u800c\u5b9e\u73b0\u590d\u6742\u7684\u66f4\u65b0\u4efb\u52a1\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5229\u7528 SQL \u7684 if test \u5224\u65ad\u5b57\u6bb5\u662f\u5426\u5728\u5217\u8868\u4e2d\uff1f\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>\u5982\u4f55\u5229\u7528 sql \u7684 if test \u6765\u5224\u65ad\u5b57\u6bb5\u662f\u5426\u5728\u5217\u8868\u4e2d \u5728\u4f7f\u7528 sql \u8bed\u53e5\u66f4\u65b0\u8bb0\u5f55\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u5224\u65ad\u67d0\u4e2a\u5b57\u6bb5\u662f\u5426\u5728\u7ed9\u5b9a\u5217\u8868\u4e2d\uff0c\u5e76\u6839\u636e\u5224\u65ad\u7ed3\u679c\u51b3\u5b9a\u662f\u5426\u66f4\u65b0\u8be5\u5b57\u6bb5\u3002\u4f20\u7edf\u7684 if-else \u8bed\u53e5\u65e0\u6cd5\u7528\u4e8e sql\uff0c\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 if test \u5b50\u53e5\u6765\u6839\u636e\u6761\u4ef6\u52a8\u6001\u751f\u6210 sql \u8bed\u53e5\u3002 \u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 if test \u5b50\u53e5\u7684\u793a\u4f8b\uff1a update table_name set field1 = case when condition1 then value1 else field1 end, field2 = case when condition2 then value2 else field2 end where condition3; \u767b\u5f55\u540e\u590d\u5236 \u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cif test \u5b50\u53e5\u7528\u4e8e\u68c0\u67e5 condition1 \u548c condition2 \u662f\u5426\u4e3a\u771f\u3002\u5982\u679c\u4e3a\u771f\uff0c\u5219\u5b57\u6bb5 field1 [&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-27817","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/27817","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=27817"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/27817\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=27817"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=27817"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=27817"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}