{"id":4527,"date":"2019-10-18T19:10:56","date_gmt":"2019-10-18T11:10:56","guid":{"rendered":"https:\/\/www.xttblog.com\/?p=4527"},"modified":"2019-10-18T19:10:56","modified_gmt":"2019-10-18T11:10:56","slug":"%e8%a7%a3%e5%86%b3-select-distinct-on-expressions-must-match-initial-order-by-expressions-%e7%9a%84%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=4527","title":{"rendered":"\u89e3\u51b3 SELECT DISTINCT ON expressions must match initial ORDER BY expressions \u7684\u6559\u7a0b"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u4e00\u822c\u6211\u4eec\u9488\u5bf9\u6392\u91cd\u6709\u4e24\u79cd\u5feb\u901f\u7684\u65b9\u6cd5\u3002\u4e00\u79cd\u662f DISTINCT\uff0c\u4e00\u79cd\u662f GROUP BY\u3002\u4f46\u662f GROUP BY \u5206\u7ec4\u4e4b\u540e\uff0c\u8981\u914d\u5408\u805a\u5408\u51fd\u6570\u4f7f\u7528\uff0c\u6bd4\u5982\uff1aSUM\u3001count\u3001avg\u3001max\u3001min \u7b49\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u5728 SELECT\u4e2d \u7684\u5217\uff0c\u6ca1\u6709\u5728 GROUP BY \u4e2d\u51fa\u73b0\uff0c\u90a3\u4e48\u8fd9\u4e2a SQL \u662f\u4e0d\u5408\u6cd5\u7684\uff0c\u56e0\u4e3a\u5217\u4e0d\u5728 GROUP BY \u4ece\u53e5\u4e2d\uff0c\u4e5f\u5c31\u662f\u8bf4\u67e5\u51fa\u6765\u7684\u5217\u5fc5\u987b\u5728 group by \u540e\u9762\u51fa\u73b0\u5426\u5219\u5c31\u4f1a\u62a5\u9519\uff0c\u6216\u8005\u8fd9\u4e2a\u5b57\u6bb5\u51fa\u73b0\u5728\u805a\u5408\u51fd\u6570\u91cc\u9762\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ed9\u4e88\u8fd9\u4e2a\u9650\u5236\uff0c\u5f88\u591a\u7f51\u53cb\u90fd\u559c\u6b22\u4f7f\u7528 DISTINCT\uff0c\u800c\u6700\u8fd1\u6709\u540c\u4e8b\u5728\u4f7f\u7528 DISTINCT \u62a5\u4e86\u9519\uff0c\u4e8e\u662f\u629b\u7ed9\u6211\uff0c\u5728\u8fd9\u91cc\u6211\u603b\u7ed3\u4e00\u4e0b\uff0c\u5206\u4eab\u7ed9\u5927\u5bb6\uff01<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>SELECT DISTINCT ON expressions must match initial ORDER BY expressions<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u6839\u636e\u8fd9\u4e2a\u9519\u8bef\uff0c\u6211\u4eec\u67e5\u8be2\u5b98\u65b9\u6587\u6863\u5f97\u77e5\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>DISTINCT ON ( expression [, \u2026] ) keeps only the first row of each set of rows where the given expressions evaluate to equal. [\u2026]\u3002 Note that the \u201cfirst row\u201d of each set is unpredictable unless ORDER BY is used to ensure that the desired row appears first. [\u2026]\u3002 The DISTINCT ON expression(s) must match the leftmost ORDER BY expression(s)\u3002<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ffb\u8bd1\u8fc7\u6765\u7684\u610f\u601d\u662f DISTINCT ON ( expression [, \u2026] )\u628a\u8bb0\u5f55\u6839\u636e[, \u2026]\u7684\u503c\u8fdb\u884c\u5206\u7ec4\uff0c\u5206\u7ec4\u4e4b\u540e\u4ec5\u8fd4\u56de\u6bcf\u4e00\u7ec4\u7684\u7b2c\u4e00\u884c\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u4f60\u4e0d\u6307\u5b9aORDER BY\u5b50\u53e5\uff0c\u8fd4\u56de\u7684\u7b2c\u4e00\u6761\u7684\u4e0d\u786e\u5b9a\u7684\u3002\u5982\u679c\u4f60\u4f7f\u7528\u4e86ORDER BY \u5b50\u53e5\uff0c\u90a3\u4e48[, \u2026]\u91cc\u9762\u7684\u503c\u5fc5\u987b\u9760\u8fd1ORDER BY\u5b50\u53e5\u7684\u6700\u5de6\u8fb9\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select distinct on(age) id,name,age,sex from xttblog;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u8fd9\u4e2a\u8bed\u53e5\uff0c\u7531\u4e8e\u6ca1\u6709\u6dfb\u52a0 ORDER BY \u5b50\u53e5\uff0c\u6240\u4ee5\u5c31\u6709\u53ef\u80fd\u5728\u6267\u884c\u65f6\u62a5 SELECT DISTINCT ON expressions must match initial ORDER BY expressions\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u53e6\u5916\u5982\u679c\u6307\u5b9a\u4e86 ORDER BY \u5b50\u53e5\uff0c\u4f46\u662f DISTINCT ON \u4e2d\u7684\u5b57\u6bb5\u4e0d\u5728 ORDER BY \u8bed\u53e5\u7684\u6700\u5de6\u8fb9\uff0c\u4e5f\u4f1a\u62a5 SELECT DISTINCT ON expressions must match initial ORDER BY expressions \u9519\u8bef\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select distinct on(age) id,name,age,sex from xttblog order by id,name;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0a\uff0c\u5e0c\u671b\u80fd\u591f\u5e2e\u52a9\u5230\u5927\u5bb6\uff01\u5982\u679c\u8fd8\u6709\u7591\u95ee\uff0c\u6b22\u8fce\u6dfb\u52a0\u6211\u7684\u5fae\u4fe1\uff0c\u4e00\u8d77\u7cbe\u8fdb\uff01<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=4527\">\u89e3\u51b3 SELECT DISTINCT ON expressions must match initial ORDER BY expressions \u7684\u6559\u7a0b<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u4e00\u822c\u6211\u4eec\u9488\u5bf9\u6392\u91cd\u6709\u4e24\u79cd\u5feb\u901f\u7684\u65b9\u6cd5\u3002\u4e00\u79cd\u662f DISTINCT\uff0c\u4e00\u79cd\u662f GROUP BY\u3002\u4f46\u662f GROUP BY  [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[2024,2026,83,2025,429],"class_list":["post-4527","post","type-post","status-publish","format-standard","hentry","category-sql","tag-distinct","tag-group-by","tag-mysql","tag-order-by","tag-postgresql"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4527","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4527"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4527\/revisions"}],"predecessor-version":[{"id":4528,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4527\/revisions\/4528"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}