{"id":221225,"date":"2020-11-26T12:23:09","date_gmt":"2020-11-26T04:23:09","guid":{"rendered":"http:\/\/4563.org\/?p=221225"},"modified":"2020-11-26T12:23:09","modified_gmt":"2020-11-26T04:23:09","slug":"mysql-case-when-%e8%af%ad%e6%b3%95%e5%a6%82%e4%bd%95%e5%b9%b6%e6%8e%a5%e5%ad%90%e6%9f%a5%e8%af%a2","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=221225","title":{"rendered":"MySQL CASE WHEN \u8bed\u6cd5\u5982\u4f55\u5e76\u63a5\u5b50\u67e5\u8be2"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  MySQL CASE WHEN \u8bed\u6cd5\u5982\u4f55\u5e76\u63a5\u5b50\u67e5\u8be2               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cs5117155 <\/span>  <span><i><\/i> 3<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<pre><code>    select amount,sid,  CASE   WHEN force_status=1 THEN  select sum(amount) as amount,sid  FROM(select amount-force_amount as amount,sid from lease_order WHERE force_status=1 and `sid` IN (7,19)) as temp GROUP BY sid   else 0 end as amount      from order; <\/code><\/pre>\n<p>\u6211\u6709\u4e00\u4e2a\u8868<code>order<\/code>,\u5f53\u5b57\u6bb5<code>force_status=1<\/code>\u65f6\u5219\u4f7f\u7528<code>force_amount-amount<\/code>\u5dee\u989d\u8fdb\u884c\u7d2f\u52a0,\u5e76\u5bf9<code>sid<\/code>\u8fdb\u884c\u5206\u7ec4\u7edf\u8ba1\uff0c\u5f53\u5b57\u6bb5<code>force_status!=1<\/code>,\u5219\u4f7f\u7528<code>amount<\/code>\u7edf\u8ba1\u5e76<code>sid<\/code>\u5206\u7ec4\u3002 \u5982\u679c\u6211\u5355\u72ec\u6267\u884c<\/p>\n<pre><code>select sum(amount) as amount,sid  FROM(select amount-force_amount as amount,sid from order WHERE force_status=1 and `sid` IN (7,19)) as temp GROUP BY sid <\/code><\/pre>\n<p>\u5219\u6ca1\u6709\u95ee\u9898\uff0c\u4f1a\u663e\u793a\u5982\u4e0b\u4fe1\u606f<\/p>\n<pre><code>amount sid 1.04 7 0.03 19 <\/code><\/pre>\n<p>\u5f53\u4f7f\u7528<code>CASE WHEN<\/code>\u5219\u62a5\u9519\uff0c\u671b\u5927\u795e\u80fd\u5e2e\u6211\u62bd\u7a7a\u770b\u770b\u662f\u54ea\u91cc\u51fa\u95ee\u9898\u4e86\u3002\u6700\u7ec8\u6548\u679c\u9700\u8981\u5b9e\u73b0\u6210<\/p>\n<pre><code>amount sid 2.03 8 0.03 19 8.03 31 9.13 20 <\/code><\/pre>\n<p>\u8fd9\u6837\u6548\u679c<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>5<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4462489\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : c6h6benzene <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             sum (case when force_status=1 then force_amount-amount else amount) group by sid                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4462490\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : c6h6benzene <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             (\u5c11\u4e86\u4e00\u4e2a end                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4462491\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : cs5117155 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @c6h6benzene \u8fd8\u662f\u4e0d\u77e5\u9053\u600e\u4e48\u5199                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4462492\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : c6h6benzene <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @cs5117155 #3 \u5c31\u2026\u5176\u5b9e\u4e0d\u9700\u8981\u5b50\u67e5\u8be2\u3002case when \u53ef\u4ee5\u5199\u5728 sum \u91cc\u9762\u7684\u3002<\/p>\n<p>select sid, sum(case when force_status=1 then force_amount-amout else amount end) from order group by sid                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4462493\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : cs5117155 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @c6h6benzene good,\u8c22\u8c22\u53ef\u4ee5\u4e86\uff0c\u7b2c\u4e00\u6b21\u5199\u8fd9\u4e2a case when \u6709\u70b9\u7ed5\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>MySQL CASE WHEN \u8bed&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/221225"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=221225"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/221225\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=221225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=221225"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=221225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}