{"id":15,"date":"2015-03-04T11:47:58","date_gmt":"2015-03-04T03:47:58","guid":{"rendered":"http:\/\/www.cxybj.com\/?p=15"},"modified":"2015-03-04T11:47:58","modified_gmt":"2015-03-04T03:47:58","slug":"mysql%e7%9a%84if-case%e8%af%ad%e5%8f%a5%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.cxybj.com\/?p=15","title":{"rendered":"MySQL\u7684if ,case\u8bed\u53e5\u4f7f\u7528\u65b9\u6cd5"},"content":{"rendered":"<p>Mysql\u7684if\u65e2\u53ef\u4ee5\u4f5c\u4e3a\u8868\u8fbe\u5f0f\u7528\uff0c\u4e5f\u53ef\u5728\u5b58\u50a8\u8fc7\u7a0b\u4e2d\u4f5c\u4e3a\u6d41\u7a0b\u63a7\u5236\u8bed\u53e5\u4f7f\u7528\uff0c\u5982\u4e0b\u662f\u505a\u4e3a\u8868\u8fbe\u5f0f\u4f7f\u7528\uff1a<\/p>\n<h2>IF\u8868\u8fbe\u5f0f<\/h2>\n<p>IF(expr1,expr2,expr3)<\/p>\n<p>\u5982\u679c expr1 \u662fTRUE (expr1 &lt;&gt; 0 and expr1 &lt;&gt; NULL)\uff0c\u5219 IF()\u7684\u8fd4\u56de\u503c\u4e3aexpr2; \u5426\u5219\u8fd4\u56de\u503c\u5219\u4e3a expr3\u3002IF() \u7684\u8fd4\u56de\u503c\u4e3a\u6570\u5b57\u503c\u6216\u5b57\u7b26\u4e32\u503c\uff0c\u5177\u4f53\u60c5\u51b5\u89c6\u5176\u6240\u5728\u8bed\u5883\u800c\u5b9a\u3002<\/p>\n<pre>select *,if(sva=1,\"\u7537\",\"\u5973\") as ssva from taname where sva != \"\"<\/pre>\n<h2>\u4f5c\u4e3a\u8868\u8fbe\u5f0f\u7684if\u4e5f\u53ef\u4ee5\u7528CASE when\u6765\u5b9e\u73b0\uff1a<\/h2>\n<pre>select CASE sva WHEN 1 THEN '\u7537' ELSE '\u5973' END as ssva from taname where sva != ''<\/pre>\n<p>\u5728\u7b2c\u4e00\u4e2a\u65b9\u6848\u7684\u8fd4\u56de\u7ed3\u679c\u4e2d\uff0c value=compare-value\u3002\u800c\u7b2c\u4e8c\u4e2a\u65b9\u6848\u7684\u8fd4\u56de\u7ed3\u679c\u662f\u7b2c\u4e00\u79cd\u60c5\u51b5\u7684\u771f\u5b9e\u7ed3\u679c\u3002\u5982\u679c\u6ca1\u6709\u5339\u914d\u7684\u7ed3\u679c\u503c\uff0c\u5219\u8fd4\u56de\u7ed3\u679c\u4e3aELSE\u540e\u7684\u7ed3\u679c\uff0c\u5982\u679c\u6ca1\u6709ELSE \u90e8\u5206\uff0c\u5219\u8fd4\u56de\u503c\u4e3a NULL\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>SELECT CASE 1 WHEN 1 THEN 'one'\r\n  WHEN 2 THEN 'two' \r\n   ELSE 'more' END\r\nas testCol<\/pre>\n<p>\u5c06\u8f93\u51faone<\/p>\n<h2>IFNULL(expr1,expr2)<\/h2>\n<p>\u5047\u5982expr1 \u4e0d\u4e3a NULL\uff0c\u5219 IFNULL() \u7684\u8fd4\u56de\u503c\u4e3a expr1; \u5426\u5219\u5176\u8fd4\u56de\u503c\u4e3a expr2\u3002IFNULL()\u7684\u8fd4\u56de\u503c\u662f\u6570\u5b57\u6216\u662f\u5b57\u7b26\u4e32\uff0c\u5177\u4f53\u60c5\u51b5\u53d6\u51b3\u4e8e\u5176\u6240\u4f7f\u7528\u7684\u8bed\u5883\u3002<\/p>\n<pre>mysql&gt; SELECT IFNULL(1,0);\r\n        -&gt; 1\r\n\r\nmysql&gt; SELECT IFNULL(NULL,10);\r\n        -&gt; 10\r\n\r\nmysql&gt; SELECT IFNULL(1\/0,10);\r\n        -&gt; 10\r\n\r\nmysql&gt; SELECT IFNULL(1\/0,'yes');\r\n        -&gt; 'yes'<\/pre>\n<p>IFNULL(expr1,expr2) \u7684\u9ed8\u8ba4\u7ed3\u679c\u503c\u4e3a\u4e24\u4e2a\u8868\u8fbe\u5f0f\u4e2d\u66f4\u52a0\u201c\u901a\u7528\u201d\u7684\u4e00\u4e2a\uff0c\u987a\u5e8f\u4e3aSTRING\u3001 REAL\u6216 INTEGER\u3002<\/p>\n<h2>IF ELSE \u505a\u4e3a\u6d41\u7a0b\u63a7\u5236\u8bed\u53e5\u4f7f\u7528<\/h2>\n<p>if\u5b9e\u73b0\u6761\u4ef6\u5224\u65ad\uff0c\u6ee1\u8db3\u4e0d\u540c\u6761\u4ef6\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\uff0c\u8fd9\u4e2a\u6211\u4eec\u53ea\u8981\u5b66\u7f16\u7a0b\u7684\u90fd\u77e5\u9053if\u7684\u4f5c\u7528\u4e86\uff0c\u4e0b\u9762\u6211\u4eec\u6765\u770b\u770bmysql \u5b58\u50a8\u8fc7\u7a0b\u4e2d\u7684if\u662f\u5982\u4f55\u4f7f\u7528\u7684\u5427\u3002<\/p>\n<pre>IF search_condition THEN \r\n    statement_list  \r\n[ELSEIF search_condition THEN]  \r\n    statement_list ...  \r\n[ELSE \r\n    statement_list]  \r\nEND IF<\/pre>\n<p>\u4e0ePHP\u4e2d\u7684IF\u8bed\u53e5\u7c7b\u4f3c\uff0c\u5f53IF\u4e2d\u6761\u4ef6search_condition\u6210\u7acb\u65f6\uff0c\u6267\u884cTHEN\u540e\u7684statement_list\u8bed\u53e5\uff0c\u5426\u5219\u5224\u65adELSEIF\u4e2d\u7684\u6761\u4ef6\uff0c\u6210\u7acb\u5219\u6267\u884c\u5176\u540e\u7684statement_list\u8bed\u53e5\uff0c\u5426\u5219\u7ee7\u7eed\u5224\u65ad\u5176\u4ed6\u5206\u652f\u3002\u5f53\u6240\u6709\u5206\u652f\u7684\u6761\u4ef6\u5747\u4e0d\u6210\u7acb\u65f6\uff0c\u6267\u884cELSE\u5206\u652f\u3002search_condition\u662f\u4e00\u4e2a\u6761\u4ef6\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u7531\u201c=\u3001&lt;\u3001&lt;=\u3001&gt;\u3001&gt;=\u3001!=\u201d\u7b49\u6761\u4ef6\u8fd0\u7b97\u7b26\u7ec4\u6210\uff0c\u5e76\u4e14\u53ef\u4ee5\u4f7f\u7528AND\u3001OR\u3001NOT\u5bf9\u591a\u4e2a\u8868\u8fbe\u5f0f\u8fdb\u884c\u7ec4\u5408\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5efa\u7acb\u4e00\u4e2a\u5b58\u50a8\u8fc7\u7a0b\uff0c\u8be5\u5b58\u50a8\u8fc7\u7a0b\u901a\u8fc7\u5b66\u751f\u5b66\u53f7\uff08student_no\uff09\u548c\u8bfe\u7a0b\u7f16\u53f7\uff08course_no\uff09\u67e5\u8be2\u5176\u6210\u7ee9\uff08grade\uff09\uff0c\u8fd4\u56de\u6210\u7ee9\u548c\u6210\u7ee9\u7684\u7b49\u7ea7\uff0c\u6210\u7ee9\u5927\u4e8e90\u5206\u7684\u4e3aA\u7ea7\uff0c\u5c0f\u4e8e90\u5206\u5927\u4e8e\u7b49\u4e8e80\u5206\u7684\u4e3aB\u7ea7\uff0c\u5c0f\u4e8e80\u5206\u5927\u4e8e\u7b49\u4e8e70\u5206\u7684\u4e3aC\u7ea7\uff0c\u4f9d\u6b21\u5230E\u7ea7\u3002\u90a3\u4e48\uff0c\u521b\u5efa\u5b58\u50a8\u8fc7\u7a0b\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>create procedure dbname.proc_getGrade  \r\n(stu_no varchar(20),cour_no varchar(10))  \r\nBEGIN \r\ndeclare stu_grade float;  \r\nselect grade into stu_grade from grade where student_no=stu_no and course_no=cour_no;  \r\nif stu_grade&gt;=90 then \r\n    select stu_grade,'A';  \r\nelseif stu_grade&lt;90 and stu_grade&gt;=80 then \r\n    select stu_grade,'B';  \r\nelseif stu_grade&lt;80 and stu_grade&gt;=70 then \r\n    select stu_grade,'C';  \r\nelseif stu_grade70 and stu_grade&gt;=60 then \r\n    select stu_grade,'D';  \r\nelse \r\n    select stu_grade,'E';  \r\nend if;  \r\nEND<\/pre>\n<p>\u6ce8\u610f\uff1aIF\u4f5c\u4e3a\u4e00\u6761\u8bed\u53e5\uff0c\u5728END IF\u540e\u9700\u8981\u52a0\u4e0a\u5206\u53f7\u201c;\u201d\u4ee5\u8868\u793a\u8bed\u53e5\u7ed3\u675f\uff0c\u5176\u4ed6\u8bed\u53e5\u5982CASE\u3001LOOP\u7b49\u4e5f\u662f\u76f8\u540c\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Mysql\u7684if\u65e2\u53ef\u4ee5\u4f5c\u4e3a\u8868\u8fbe\u5f0f\u7528\uff0c\u4e5f\u53ef\u5728\u5b58\u50a8\u8fc7\u7a0b\u4e2d\u4f5c\u4e3a\u6d41\u7a0b &hellip; <a href=\"https:\/\/www.cxybj.com\/?p=15\" class=\"more-link\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[10,11],"_links":{"self":[{"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/posts\/15"}],"collection":[{"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=15"}],"version-history":[{"count":1,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/posts\/15\/revisions"}],"predecessor-version":[{"id":16,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/posts\/15\/revisions\/16"}],"wp:attachment":[{"href":"https:\/\/www.cxybj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=15"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=15"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=15"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}