{"id":98506,"date":"2020-03-27T22:53:36","date_gmt":"2020-03-27T14:53:36","guid":{"rendered":"http:\/\/4563.org\/?p=98506"},"modified":"2020-03-27T22:53:36","modified_gmt":"2020-03-27T14:53:36","slug":"%e8%af%b7%e6%95%99%ef%bc%9a%e4%bb%8e%e4%b8%80%e5%8f%a5-sql-%e8%bf%94%e5%9b%9e%e7%9a%84-id-%e5%88%97%e8%a1%a8%e9%81%8d%e5%8e%86%e6%9f%a5%e8%af%a2%e5%8f%a6%e4%b8%80-sql-%e8%af%ad%e5%8f%a5","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=98506","title":{"rendered":"\u8bf7\u6559\uff1a\u4ece\u4e00\u53e5 sql \u8fd4\u56de\u7684 id \u5217\u8868\u904d\u5386\u67e5\u8be2\u53e6\u4e00 sql \u8bed\u53e5"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u8bf7\u6559\uff1a\u4ece\u4e00\u53e5 sql \u8fd4\u56de\u7684 id \u5217\u8868\u904d\u5386\u67e5\u8be2\u53e6\u4e00 sql \u8bed\u53e5               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ShawyerPeng <\/span>  <span><i><\/i> 44<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u6709\u4e24\u4e2a\u8868 payment \u548c cost\uff0cpayment \u8868\u6709\u4e00\u4e2a type \u5b57\u6bb5\u7528\u4e8e\u67d0\u4e2a\u4e1a\u52a1\u573a\u666f\u533a\u5206\u6c42\u548c\u65f6\u662f\u52a0\u8fd8\u662f\u51cf\uff0corderid = 123 \u7684\u5355\u6761 sql \u8bed\u53e5\u53ef\u4ee5\u5199\u6210\u5982\u4e0b\uff1a<\/p>\n<pre><code>SELECT (     (SELECT sum(amount)         FROM payment f         WHERE orderid = 123 AND providerid = 456 AND amount &lt;&gt; 0 AND type = 2) -     (SELECT sum(amount)         FROM payment         WHERE orderid = 123 AND providerid = 456 AND amount &lt;&gt; 0 AND type = 1) -     (SELECT sum(cost)         FROM cost         WHERE suborderid in (SELECT suborderid FROM suborder WHERE orderid = 123)) ) <\/code><\/pre>\n<p>\u8bf7\u6559\u4e00\u4e0b\u662f\u5426\u6709\u7eaf SQL \u8bed\u53e5\u65b9\u5f0f\u5b9e\u73b0\u4ece\u4e00\u4e2a\u96c6\u5408\uff08\u901a\u8fc7\u8bed\u53e5<code>SELECT DISTINCT orderid FROM payment<\/code>\u5f97\u5230\u6240\u6709\u7684 orderid \uff09\u904d\u5386\u67e5\u8be2\u51fa\u6bcf\u4e2a orderid \u5bf9\u5e94\u7684 sum \u6c42\u548c\u7ed3\u679c\u3002<\/p>\n<p>\u6709\u5c1d\u8bd5\u7528\u8fc7\u6e38\u6807\u548c\u81ea\u8fde\u63a5\u7684\u65b9\u5f0f\uff0c\u4f46\u7ed3\u679c\u597d\u50cf\u4e0d\u5bf9\u3002<\/p>\n<pre><code>DECLARE @OrderId BIGINT DECLARE My_Cursor CURSOR     FOR (SELECT DISTINCT OrderID FROM payment) OPEN My_Cursor; FETCH NEXT FROM My_Cursor INTO @OrderId; WHILE @@FETCH_STATUS = 0 BEGIN     SELECT (     (SELECT sum(amount)         FROM payment f         WHERE orderid = @OrderId AND providerid = 456 AND amount &lt;&gt; 0 AND type = 2) -     (SELECT sum(amount)         FROM payment         WHERE orderid = @OrderId AND providerid = 456 AND amount &lt;&gt; 0 AND type = 1) -     (SELECT sum(cost)         FROM cost         WHERE suborderid in (SELECT suborderid FROM suborder WHERE orderid = @OrderId))     FETCH NEXT FROM My_Cursor INTO @OrderId; END CLOSE My_Cursor; DEALLOCATE My_Cursor; GO; <\/code><\/pre>\n<pre><code>SELECT f.OrderID, p.suborderid, SUM(CASE WHEN f.type = 2 THEN f.amount END) - SUM(CASE WHEN f.type = 1 THEN f.amount END) - SUM(p.Cost) FROM payment f,      cost p,      suborder s WHERE f.OrderID = s.OrderID   AND p.SubOrderID = s.SubOrderID   AND f.ProviderID = 456   AND f.amount &lt;&gt; 0 group by f.OrderID, p.SubOrderID; <\/code><\/pre>\n<p>\u611f\u8c22\u5404\u4f4d\u5927\u4f6c~<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>1<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"712652\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ccgoing10 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6700\u540e\u9762\u90a3\u4e2a sql \u5728 group by \u7684\u65f6\u5019\u628a SubOrderID \u53bb\u6389\u8bd5\u8bd5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u8bf7\u6559\uff1a\u4ece\u4e00\u53e5 sql \u8fd4\u56de\u7684 id&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\/98506"}],"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=98506"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/98506\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=98506"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=98506"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=98506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}