{"id":130982,"date":"2018-12-21T09:09:21","date_gmt":"2018-12-21T01:09:21","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=130982"},"modified":"2018-12-18T10:11:33","modified_gmt":"2018-12-18T02:11:33","slug":"mysql-select","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/mysql-select.html","title":{"rendered":"\u5b66\u4e60MySQL\u7684select\u8bed\u53e5"},"content":{"rendered":"
select\u8bed\u53e5\u53ef \u4ee5\u7528\u56de\u8f66\u5206\u9694 <\/strong><\/span><\/div>\n
\r\n$sql=\"select * from article where id=1\"\u548c  $sql=\"select * from article where id=1\"\r\n\u90fd\u53ef\u4ee5\u5f97\u5230\u6b63\u786e\u7684\u7ed3\u679c\uff0c\u4f46\u6709\u65f6\u5206\u5f00\u5199\u6216\u8bb8\u80fd \u66f4\u660e\u4e86\u4e00\u70b9\uff0c\u7279\u522b\u662f\u5f53sql\u8bed\u53e5\u6bd4\u8f83\u957f\u65f6\u3002\r\n<\/pre>\n
\u6279\u91cf\u67e5\u8be2\u6570\u636e<\/strong><\/span><\/div>\n
\r\n\u53ef\u4ee5\u7528in \u6765\u5b9e\u73b0 \r\n$sql=\"select * from article where id  ;in(1,3,5)\"\r\n<\/pre>\n
\u4f7f\u7528concat\u8fde\u63a5\u67e5\u8be2\u7684\u7ed3\u679c<\/strong><\/span><\/div>\n
\r\n$sql=\"select concat(id,\"-\",con)  as res from article where id=1\"\r\n\u8fd4\u56de \"1-article content\"\r\n<\/pre>\n
\u4f7f\u7528locate<\/strong><\/span><\/div>\n
\r\n\u7528\u6cd5\uff1a select locate(\"hello\",\"hello baby\");\u8fd4\u56de1\r\n\u4e0d\u5b58\u5728\u8fd4\u56de 0\r\n<\/pre>\n
\u4f7f\u7528group by<\/strong><\/span><\/div>\n
\r\n\u4ee5\u524d\u4e00\u76f4\u6ca1\u600e\u4e48\u641e\u660egroup by  \u548c order by\uff0c\u5176\u5b9e\u4e5f\u6ee1\u7b80\u5355\u7684\uff0cgroup by \u662f\u628a\u76f8\u540c\u7684\u7ed3 \u679c\u7f16\u4e3a\u4e00\u7ec4\r\nexam\uff1a$sql=\"select city ,count(*)  from customer group by city\";\r\n\u8fd9\u53e5\u8bdd\u7684\u610f \u601d\u5c31\u662f\u4ececustomer\u8868\u91cc\u5217\u51fa\u6240\u6709\u4e0d\u91cd\u590d\u7684\u57ce\u5e02\uff0c\u53ca\u5176\u6570\u91cf\uff08\u6709\u70b9\u7c7b\u4f3cdistinct\uff09 \r\ngroup by \u7ecf\u5e38\u4e0eAVG(),MIN(),MAX(),SUM(),COUNT()\u4e00\u8d77\u4f7f\u7528 \r\n<\/pre>\n
\u4f7f\u7528having<\/strong><\/span><\/div>\n
\r\nhaving \u5141\u8bb8\u6709\u6761\u4ef6\u5730\u805a\u5408\u6570\u636e\u4e3a\u7ec4 \r\n$sql=\"select city,count(*),min(birth_day)  from customer\r\ngroup by city having c ount(*)>10\";\r\n\u8fd9\u53e5\u8bdd\u662f\u5148\u6309city\u5f52\u7ec4\uff0c\u7136\u540e\u627e\u51facity\u5730\u6570\u91cf\u5927\u4e8e10\u7684\u57ce \u5e02\r\nbtw\uff1a\u4f7f\u7528group by + having \u901f\u5ea6\u6709\u70b9\u6162\r\n\u540c \u65f6having\u5b50\u53e5\u5305\u542b\u7684\u8868\u8fbe\u5f0f\u5fc5\u987b\u5728\u4e4b\u524d\u51fa\u73b0\u8fc7\r\n<\/pre>\n
\u7ec4\u5408\u5b50\u53e5<\/strong><\/span><\/div>\n
\r\nwhere\u3001 group by\u3001having\u3001order by\uff08\u5982\u679c\u8fd9\u56db\u4e2a\u90fd\u8981\u4f7f\u7528\u7684\u8bdd\uff0c\u4e00\u822c\u6309\u8fd9 \u4e2a\u987a\u5e8f\u6392\u5217\uff09\r\n<\/pre>\n
\u4f7f\u7528distinct<\/strong><\/span><\/div>\n
\r\ndistinct\u662f\u53bb\u6389\u91cd\u590d\u503c\u7528\u7684 \r\n$sql=\"select distinct city from customer  order by id desc\";\r\n\u8fd9\u53e5\u8bdd\u7684\u610f\u601d\u5c31\u662f\u4ececustomer\u8868\u4e2d\u67e5 \u8be2\u6240\u6709\u7684\u4e0d\u91cd\u590d\u7684city\r\n<\/pre>\n
\u4f7f\u7528limit<\/strong><\/span><\/div>\n
\r\n\u5982\u679c\u8981\u663e\u793a\u67d0\u6761\u8bb0\u5f55\u4e4b\u540e\u7684\u6240 \u6709\u8bb0\u5f55 \r\n$sql=\"select * from article limit 100,- 1\";\r\n<\/pre>\n
\u591a\u8868\u67e5\u8be2 <\/strong><\/span><\/div>\n
\r\n$sql=\"select user_name from user  u,membe r  m\r\nwhere u.id=m.id and \r\nm.reg_date> ;=2006-12-28\r\norder by u.id desc\"\r\n<\/pre>\n

\u6ce8\u610f\uff1a\u5982\u679cuser\u548c member\u4e24\u4e2a\u6807\u540c\u65f6\u6709user_name\u5b57\u6bb5\uff0c\u4f1a\u51fa\u73b0mysql\u9519\u8bef\uff08\u56e0\u4e3amysql\u4e0d\u77e5\u9053\u4f60\u5230 \u5e95\u8981\u67e5\u8be2\u54ea\u4e2a\u8868\u91cc\u7684user_name\uff09\uff0c\u5fc5\u987b\u6307\u660e\u662f\u54ea\u4e2a\u8868\u7684\uff1b<\/p>\n","protected":false},"excerpt":{"rendered":"

$sql=”select * from article where id=1″\u548c $sql=”select * […]<\/p>\n","protected":false},"author":668,"featured_media":15758,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-130982","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/130982","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/668"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=130982"}],"version-history":[{"count":6,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/130982\/revisions"}],"predecessor-version":[{"id":131025,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/130982\/revisions\/131025"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/15758"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=130982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=130982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=130982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}