{"id":10878,"date":"2023-02-17T16:47:32","date_gmt":"2023-02-17T08:47:32","guid":{"rendered":"https:\/\/blog.hoyo.idv.tw\/?p=10878"},"modified":"2023-08-30T13:40:35","modified_gmt":"2023-08-30T05:40:35","slug":"mysql-%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/blog.hoyo.idv.tw\/?p=10878","title":{"rendered":"MySQL - \u6392\u5e8f"},"content":{"rendered":"<p>--<\/p>\n<h2>1-1-1 \u6392\u5e8f<\/h2>\n<pre class=\"lang:default decode:true\">ORDER BY CHAR_LENGTH(question_code) DESC, question_code DESC;<\/pre>\n<p><img loading=\"lazy\" class=\"alignnone size-full wp-image-12453\" src=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2023\/02\/2023-08-14-17-30-57.png\" alt=\"\" width=\"154\" height=\"305\" srcset=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2023\/02\/2023-08-14-17-30-57.png 154w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2023\/02\/2023-08-14-17-30-57-151x300.png 151w\" sizes=\"(max-width: 154px) 100vw, 154px\" \/><\/p>\n<p>--<\/p>\n<h2>\u4f7f\u7528\u4e2d\u6587\u7b46\u5283\u9806\u5e8f<\/h2>\n<pre class=\"lang:default decode:true \">CONVERT(name using big5)<\/pre>\n<p>--<\/p>\n<h2>\u6578\u5b57 + \u4e2d\u6587\u7684\u6392\u5e8f<\/h2>\n<ul>\n<li><a href=\"https:\/\/electrictoolbox.com\/mysql-order-string-as-int\/\" target=\"_blank\" rel=\"noopener\">MySQL: order a string column as an integer<\/a><\/li>\n<li><a class=\"question-hyperlink\" href=\"https:\/\/stackoverflow.com\/questions\/33218636\/mysql-order-by-convert-varchar-as-unsigned-and-sort-0-to-the-last\" target=\"_blank\" rel=\"noopener\">MySQL order by convert varchar as unsigned and sort \"0\" to the last<\/a><\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">ORDER BY CAST(name AS UNSIGNED) DESC<\/pre>\n<p>--<\/p>\n<h2>\u81ea\u5b9a\u7fa9\u6392\u5e8f\u9806\u5e8f<\/h2>\n<ul>\n<li><a class=\"question-hyperlink\" href=\"https:\/\/stackoverflow.com\/questions\/9378613\/how-to-define-a-custom-order-by-order-in-mysql\">How to define a custom ORDER BY order in mySQL<\/a><\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">ORDER BY FIELD(Language,'ENU','JPN','DAN'), ID<\/pre>\n<p>--<\/p>\n<h2>\u6839\u64da LIKE \u81ea\u5b9a\u7fa9\u9806\u5e8f<\/h2>\n<ul>\n<li><a class=\"question-hyperlink\" href=\"https:\/\/stackoverflow.com\/questions\/3609166\/mysql-order-by-like\">Mysql: Order by like?<\/a><\/li>\n<\/ul>\n<pre class=\"lang:default decode:true \">ORDER BY CASE \r\n    WHEN name LIKE \"%John%\" THEN 1 \r\n    WHEN name LIKE \"%Doe%\"  THEN 2 \r\n    ELSE 3 \r\nEND<\/pre>\n<p>--<\/p>\n<h2>\u6839\u64da\u67d0\u6bb5\u5167\u5bb9\u6392\u5e8f<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.tutorialspoint.com\/how-to-order-by-certain-part-of-a-string-in-mysql\" target=\"_blank\" rel=\"noopener\">How to order by certain part of a string in MySQL? (tutorialspoint.com)<\/a><\/li>\n<li><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/string-functions.html#function_substring-index\" target=\"_blank\" rel=\"noopener\">MySQL :: MySQL 8.0 Reference Manual :: 12.8 String Functions and Operators<\/a><\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">ORDER BY \r\n    SUBSTRING(exam_source, 5, 3) DESC,\r\n    SUBSTRING(exam_source, 9, 1) DESC;<\/pre>\n<p>\u5b57\u4e32\u7684\u64f7\u53d6\u6709\u591a\u500b function \u8acb\u6839\u64da\u9700\u6c42\u53d6\u7528<\/p>\n<p>--<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"10878\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;3,066&nbsp;total views, &nbsp;2&nbsp;views today<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>-- 1-1-1 \u6392\u5e8f ORD...<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"10878\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;3,066&nbsp;total views, &nbsp;2&nbsp;views today<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[258],"tags":[349],"_links":{"self":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/10878"}],"collection":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10878"}],"version-history":[{"count":10,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/10878\/revisions"}],"predecessor-version":[{"id":12217,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/10878\/revisions\/12217"}],"wp:attachment":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}