???MySQL?????????
???????????????????????????????
???????????ο??鼮?????????????????????????????????

??????????????????????????????????????????MySQLD????????‘SQL Layer’???2???????????????????洢???棬?????‘Storage Engine Layer’?????????????????????????????????????????????????????????
??????????????
????????????????Щ????????????????????2????е??????????????
????1.????
????1.1???????????Query??????????????‘??????????’????????
????1.2???????????‘?????/??????’
????1.3????‘??????’????????????
????1.4???????‘?????/??????’??‘????????’????????е????????????????????????????????????????μ?????????
????2.????
????2.1???????棬???Query????????????????????????????????????????????????
????2.2?????????????????‘?????????’????????????????????????????????
????2.3??????????????Σ?????????????????????壬?????????????μ??????
????2.4??????????????鴦??
????2.5?????SELECT?????????‘????????’???????????????????м??
????2.6??????????????‘??????????’?????????????????з???????????????????
????2.7???????‘????????’???????table cache???????????????????????????????????′??????
????2.8??????meta???????????洢???????????????????????????洢???洦??
????2.9?????????в???????仯????????????????????????????????????????
????3.???
????3.1Query????????????????????‘?????/??????’
????3.2??????????????????????????????????
????3.3‘?????/??????’???к?????????????????????????????????????????
??????С???

????SQL???????
???????????????????????????????SQL?????????????
??????????????????
????SELECT DISTINCT
????< select_list >
????FROM
????< left_table > < join_type >
????JOIN < right_table > ON < join_condition >
????WHERE
????< where_condition >
????GROUP BY
????< group_by_list >
????HAVING
????< having_condition >
????ORDER BY
????< order_by_condition >
????LIMIT < limit_number >
?????????????????????????
????1 FROM <left_table>
????2 ON <join_condition>
????3 <join_type> JOIN <right_table>
????4 WHERE <where_condition>
????5 GROUP BY <group_by_list>
????6 HAVING <having_condition>
????7 SELECT
????8 DISTINCT <select_list>
????9 ORDER BY <order_by_condition>
????10 LIMIT <limit_number>
???????????????????????????????????????г???????????????????????????????????????????????????????????????
???????????????????????????????????е????ɡ?
???????????
????1.?????????????
????create database testQuery
????2.?????????
????CREATE TABLE table1
????(
????uid VARCHAR(10) NOT NULL??
????name VARCHAR(10) NOT NULL??
????PRIMARY KEY(uid)
????)ENGINE=INNODB DEFAULT CHARSET=UTF8;
????CREATE TABLE table2
????(
????oid INT NOT NULL auto_increment??
????uid VARCHAR(10)??
????PRIMARY KEY(oid)
????)ENGINE=INNODB DEFAULT CHARSET=UTF8;
????3.????????
????INSERT INTO table1(uid??name) VALUES('aaa'??'mike')??('bbb'??'jack')??('ccc'??'mike')??('ddd'??'mike');
????INSERT INTO table2(uid) VALUES('aaa')??('aaa')??('bbb')??('bbb')??('bbb')??('ccc')??(NULL);
????4.?????????
????SELECT
????a.uid??
????count(b.oid) AS total
????FROM
????table1 AS a
????LEFT JOIN table2 AS b ON a.uid = b.uid
????WHERE
????a. NAME = 'mike'
????GROUP BY
????a.uid
????HAVING
????count(b.oid) < 2
????ORDER BY
????total DESC
????LIMIT 1;