{"id":148,"date":"2016-07-07T00:07:25","date_gmt":"2016-07-06T16:07:25","guid":{"rendered":"http:\/\/www.sunpengcheng.com\/?p=148"},"modified":"2016-07-09T23:07:48","modified_gmt":"2016-07-09T15:07:48","slug":"here-are-some-tips-for-sql","status":"publish","type":"post","link":"https:\/\/www.sunpengcheng.com\/index.php\/2016\/07\/07\/here-are-some-tips-for-sql\/","title":{"rendered":"Here are some tips for SQL"},"content":{"rendered":"<h3>SQL Server<\/h3>\n<p>Concatenate rows into a comma-separated list,we can do it like this.<\/p>\n<p>[tsql]<br \/>\nSELECT DISTINCT<br \/>\n\tusr.id, usr.name,<br \/>\n\t(SELECT STUFF((SELECT &#8216;,&#8217;+title FROM hobby_main AS hm WHERE hm.uid=usr.id FOR XML PATH(&#8221;)),1,1,&#8221;)) AS hobbies<br \/>\nFROM user AS usr<br \/>\nWHERE name = &#8216;Pengcheng&#8217;<br \/>\n[\/tsql]<\/p>\n<h3>MySQL<\/h3>\n<p>I help my little brother to rank the running data in CampusRun app,there is a release date for this app.It calculates total distance from the release date when user&#8217;s registration date before the app release date,then calculates total distance from user&#8217;s registration date.so i have to use IF statement inside WHERE clause in SQL like below.<\/p>\n<p>[mysql]<br \/>\nSELECT b.id, b.cnname, SUM(distance)\/1000 AS total_distance<br \/>\nFROM `xm__run_infos_by_day` AS a<br \/>\n\tINNER JOIN `xm__member` AS b<br \/>\n\tON a.ydq_openid = b.ydq_openid<br \/>\nWHERE IF(b.ydq_join_time < '1467450000', a.time >= &#8216;1467450000&#8217;, a.time >= b.ydq_join_time)<br \/>\nGROUP BY a.ydq_openid, b.cnname, b.id<br \/>\nORDER BY SUM(distance)\/1000 DESC<br \/>\n[\/mysql]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SQL Server Concatenate rows into a comma-separated list<\/p>\n<p><a class=\"moretag\" href=\"https:\/\/www.sunpengcheng.com\/index.php\/2016\/07\/07\/here-are-some-tips-for-sql\/\">Continue Reading&hellip;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44,6],"tags":[43,45],"class_list":["post-148","post","type-post","status-publish","format-standard","hentry","category-mysql","category-sql-server","tag-concatenate","tag-where-if"],"_links":{"self":[{"href":"https:\/\/www.sunpengcheng.com\/index.php\/wp-json\/wp\/v2\/posts\/148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sunpengcheng.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sunpengcheng.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sunpengcheng.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sunpengcheng.com\/index.php\/wp-json\/wp\/v2\/comments?post=148"}],"version-history":[{"count":6,"href":"https:\/\/www.sunpengcheng.com\/index.php\/wp-json\/wp\/v2\/posts\/148\/revisions"}],"predecessor-version":[{"id":154,"href":"https:\/\/www.sunpengcheng.com\/index.php\/wp-json\/wp\/v2\/posts\/148\/revisions\/154"}],"wp:attachment":[{"href":"https:\/\/www.sunpengcheng.com\/index.php\/wp-json\/wp\/v2\/media?parent=148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sunpengcheng.com\/index.php\/wp-json\/wp\/v2\/categories?post=148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sunpengcheng.com\/index.php\/wp-json\/wp\/v2\/tags?post=148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}