¿ìËÙÕÆÎÕMySQLÊý¾Ý¿âÖÐSELECTÓï¾ä
¡¡MySQLÖÐSELECTÓï¾äµÄ»ù±¾Óï·¨ÊÇ£º
ÒÔÏÂÊÇÒýÓÃƬ¶Î£º SELECT [STRAIGHT_JOIN] [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [HIGH_PRIORITY] [DISTINCT|DISTINCTROW|ALL] select_list [INTO {OUTFILE|DUMPFILE} 'file_name' export_options] [FROM table_references [WHERE where_definition] [GROUP BY col_name,...] [HAVING where_definition] [ORDER BY {unsighed_integer|col_name|formura} [ASC|DESC],...] [LIMIT [offset,] rows] [PROCEDURE procedure_name]]
¡¡¡¡´ÓÕâ¸ö»ù±¾Óï·¨¿ÉÒÔ¿´³ö£¬×î¼òµ¥µÄSELECTÓï¾äÊÇSELECT select_list£¬Êµ¼ÊÉÏÀûÓÃÕâ¸ö×î¼òµ¥µÄSELECTÓï¾ä£¬ÄãÒ²¿ÉÒÔÍê³ÉÐí¶àÄãÆÚ´ýµÄ¹¦ÄÜ£¬Ê×ÏÈÄãÄÜÀûÓÃËü½øÐÐMySQLËùÖ§³ÖµÄÈκÎÔËË㣬ÀýÈ磺SELECT 1+1£¬Ëü½«·µ»Ø2;Æä´Î£¬ÄãÒ²ÄÜÀûÓÃËü¸ø±äÁ¿¸³Öµ£¬¶øÔÚPHPÖУ¬ÔËÓÃSELECTÓï¾äµÄÕâÖÖ¹¦ÄÜ£¬Äã¾Í¿ÉÒÔ×ÔÓɵØÔËÓÃMySQLµÄº¯ÊýΪPHP³ÌÐò½øÐи÷ÖÖÔËË㣬²¢¸³Öµ¸ø±äÁ¿¡£ÔںܶàµÄʱºò£¬Äã»á·¢ÏÖMySQLÓµÓÐÐí¶à±ÈPHP¸üΪ¹¦ÄÜÇ¿´óµÄº¯Êý¡£
¡¡¡¡STRAIGHT_JOIN¡¢SQL_SMALL_RESULT¡¢SQL_BIG_RESULT¡¢HIGH_PRIORITYÊÇMySQL¶ÔANSI SQL92µÄÀ©Õ¹¡£Èç¹ûÓÅ»¯Æ÷ÒÔ·Ç×î¼Ñ´ÎÐòÁª½á±í£¬Ê¹ÓÃSTRAIGHT_JOIN¿ÉÒÔ¼Ó¿ì²éѯ¡£
¡¡¡¡SQL_SMALL_RESULTºÍSQL_BIG_RESULTÊÇÒ»×éÏà¶ÔµÄ¹Ø¼ü´Ê¡£ËüÃDZØÐëÓëGROUP BY¡¢DISTINCT»òDISTINCTROWÒ»ÆðʹÓá£SQL_SMALL_RESULT¸æÖªÓÅ»¯Æ÷½á¹û»áºÜС£¬ÒªÇóMySQLʹÓÃÁÙʱ±í´æ´¢×îÖյıí¶ø²»ÊÇʹÓÃÅÅÐò;·´Ö®£¬SQL_BIG_RESULT¸æÖªÓÅ»¯Æ÷½á¹û»áºÜС£¬ÒªÇóMySQLʹÓÃÅÅÐò¶ø²»ÊÇ×öÁÙʱ±í¡£
¡¡¡¡HIGH_PRIORITY½«¸³ÓèSELECT±ÈÒ»¸ö¸üбíµÄÓï¾ä¸ü¸ßµÄÓÅÏȼ¶£¬Ê¹Ö®¿ÉÒÔ½øÐÐÒ»´ÎÓÅÏȵĿìËٵIJéѯ¡£
¡¡¡¡ÒÔÉÏËĸö¹Ø¼ü´ÊµÄʹÓ÷½·¨µÄÈ·±È½Ï»Þɬ¡£ÐÒÔ˵ÄÊÇ£¬ÔÚ¾ø´ó¶àÊýÇé¿öÏ£¬ÔÚMySQLÖÐÎÒÃÇÍêÈ«¿ÉÒÔÑ¡Ôñ²»Ê¹ÓÃÕâËĸö¹Ø¼ü´Ê¡£
¡¡¡¡DISTINCT¡¢DISTINCTROW¶Ô²éѯ·µ»ØµÄ½á¹û¼¯ÌṩÁËÒ»¸ö×î»ù±¾µ«ÊǺÜÓÐÓõĹýÂË¡£ÄǾÍÊǽá¹û¼¯ÖÐÖ»º¬·ÇÖظ´ÐС£ÔÚÕâÀïҪעÒâµÄÊÇ£¬¶Ô¹Ø¼ü´ÊDISTINCT¡¢DISTINCTROWÀ´Ëµ£¬¿ÕÖµ¶¼ÊÇÏàµÈµÄ£¬ÎÞÂÛÓжàÉÙNULLÖµ£¬Ö»Ñ¡ÔñÒ»¸ö¡£¶øALLµÄÓ÷¨¾ÍÓлÉßÌí×ãÖ®ÏÓÁË¡£Ëü¶Ô½á¹û¼¯µÄ²úÉúûÓÐÈκÎÓ°Ïì¡£
¡¡¡¡INTO {OUTFILE|DUMPFILE} 'file_name' export_options£¬½«½á¹û¼¯Ð´ÈëÒ»¸öÎļþ¡£ÎļþÔÚ·þÎñÆ÷Ö÷»úÉϱ»´´½¨£¬²¢ÇÒ²»ÄÜÊÇÒѾ´æÔڵġ£Óï¾äÖеÄexport_options²¿·ÖµÄÓï·¨ÓëÓÃÔÚLOAD DATAINFILEÓï¾äÖеÄFIELDSºÍLINES×Ó¾äÖеÄÏàͬ£¬ÎÒÃǽ«ÔÚMySQL½ø½×_LOAD DATAƪÖÐÏêϸÌÖÂÛËü¡£¶øOUTFILEÓëDUMPFILEµÄ¹Ø¼ü×ÖµÄÇø±ðÊÇ£ººóǰֻдһÐе½Îļþ£¬²¢Ã»ÓÐÈκÎÁлòÐнáÊø¡£
¡¡¡¡select list£ºÆäÖпÉÒÔ°üº¬Ò»Ïî»ò¶àÏîÏÂÁÐÄÚÈÝ£º
¡¡¡¡1¡¢¡°*¡±£¬±íʾ°´ÕÕcreate tableµÄ˳ÐòÅÅÁеÄËùÓÐÁС£
¡¡¡¡2¡¢°´ÕÕÓû§ËùÐè˳ÐòÅÅÁеÄÁÐÃûµÄÇåµ¥¡£
¡¡¡¡3¡¢¿ÉÒÔʹÓñðÃûÈ¡´úÁÐÃû£¬ÐÎʽÈçÏ£ºcolumn name as column_heading¡£
¡¡¡¡4¡¢±í´ïʽ(ÁÐÃû¡¢³£Á¿¡¢º¯Êý£¬»òÒÔËãÊõ»òÖðλÔËËã·ûÁ¬½ÓµÄÁÐÃû¡¢³£Á¿ºÍº¯ÊýµÄÈκÎ×éºÏ)¡£
¡¡¡¡5¡¢ÄÚ²¿º¯Êý»ò¼¯ºÏº¯Êý¡£
¡¡¡¡6¡¢ÉÏÊö¸÷ÏîµÄÈκÎÒ»ÖÖ×éºÏ¡£
¡¡¡¡FROM£º¾ö¶¨SELECTÃüÁîÖÐʹÓÃÄÄЩ±í¡£Ò»°ã¶¼ÒªÇóÓдËÏ³ý·Çselect_listÖв»º¬ÁÐÃû(ÀýÈ磬ֻÓг£Á¿¡¢ËãÊõ±í´ïʽµÈ)¡£Èç¹û±íÏîÖÐÓжà¸ö±í£¬ÓöººÅ½«Ö®·Ö¿ª¡£Ôڹؼü´ÊFROMºóÃæµÄ±íµÄ˳Ðò²»Ó°Ïì½á¹û¡£
¡¡¡¡±íÃû¿ÉÒÔ¸ø³öÏà¹Ø±ðÃû£¬ÒÔ±ãʹ±í´ïÇåÎú¡£ÕâÀïµÄÓï·¨ÊÇtbl_name [AS] alias_name¡£ÀýÈ磺
¡¡¡¡select t1.name,t2.salary from employee as t1,info as t2 where t1.name=t2.nameÓëselect t1.name,t2.salary from employee t1,info t2 where t1.name=t2.nameÊÇÍêÈ«µÈ¼ÛµÄ¡£
¡¡¡¡ËùÓжԸñíµÄÆäËûÒýÓã¬ÀýÈçÔÚwhere×Ó¾äºÍhaving×Ó¾äÖУ¬¶¼ÒªÓñðÃû£¬±ðÃû²»ÄÜÒÔÊý×Ö¿ªÍ·¡£
¡¡¡¡where×Ó¾äÉèÖÃÁËËÑË÷Ìõ¼þ£¬ËüÔÚinsert£¬update£¬deleteÓï¾äÖеÄÓ¦Ó÷½·¨Ò²ÓëÔÚselectÓï¾äÖеÄÓ¦Ó÷½·¨ÍêÈ«Ïàͬ¡£ËÑË÷Ìõ¼þ½ô¸úÔڹؼü´ÊwhereµÄºóÃæ¡£Èç¹ûÓû§ÒªÔÚÓï¾äÖÐʹÓöà¸öËÑË÷Ìõ¼þ£¬Ôò¿ÉÓÃand»òorÁ¬½Ó¡£ËÑË÷Ìõ¼þµÄ»ù±¾Óï·¨ÊÇ[not] expression comparison_operator expression;[not] expression [not] like ¡°match_string¡±;[not] expression is [not] null;[not] expression [not] between expression and expression;[not] column_name join_operator column_name;[not] boolean_expression¡£
¡¡¡¡and£ºÓÃÀ´Áª½áÁ½¸öÌõ¼þ£¬²¢ÔÚÁ½¸öÌõ¼þ¶¼ÊÇTRUEµÄʱºò·µ»Ø½á¹û¡£µ±ÔÚͬһÓï¾äÖÐʹÓöà¸öÂß¼ÔËËã·ûʱ£¬andÔËËã·û×ÜÊÇ×îÓÅÏÈ£¬³ý·ÇÓû§ÓÃÀ¨ºÅ¸Ä±äÁËÔËËã˳Ðò¡£
¡¡¡¡or£ºÓÃÀ´Áª½áÁ½¸öÌõ¼þ£¬µ±Á½¸öÌõ¼þÖÐÓÐÈÎÒ»Ìõ¼þÊÇTRUEµÄʱºò·µ»Ø½á¹û¡£µ±ÔÚͬһÓï¾äÖÐʹÓöà¸öÂß¼ÔËËã·ûʱ£¬ÔËËã·ûorͨ³£ÔÚÔËËã·ûandÖ®ºó½øÐÐÔËËã¡£µ±È»Óû§¿ÉÒÔʹÓÃÀ¨ºÅ¸Ä±äÔËËãµÄ˳Ðò¡£
¡¡¡¡between£ºÓÃÀ´±êʶ·¶Î§ÏÂÏ޵Ĺؼü´Ê£¬andºóÃæ¸ú·¶Î§ÉÏÏÞµÄÖµ¡£·¶Î§where @val between x and y°üº¬Ê×βֵ¡£Èç¹ûbetweenºóÃæÖ¸¶¨µÄµÚÒ»¸öÖµ´óÓÚµÚ¶þ¸öÖµ£¬Ôò¸Ã²éѯ²»·µ»ØÈκÎÐС£
¡¡¡¡column_name£ºÔڱȽÏÖÐʹÓõÄÁÐÃû¡£ÔÚ»á²úÉúÆçÒåʱ£¬Ò»¶¨ÒªÖ¸Ã÷ÁÐËùÔڵıíÃû¡£
¡¡¡¡comparison_operator£º±È½ÏÔËËã·û¡£¼ûÏÂ±í£º
ÒÔÏÂÊÇÒýÓÃƬ¶Î£º ·ûºÅ ÒâÒå = µÈÓÚ > ´óÓÚ < СÓÚ >= ´óÓÚµÈÓÚ <= СÓÚµÈÓÚ != ²»µÈÓÚ <> ²»µÈÓÚ
¡¡¡¡ÔڱȽÏchar£¬varcharÐÍÊý¾Ýʱ£¬¡°<¡±µÄÒâ˼ÊǸü½Ó½ü×Öĸ±íÍ·²¿£¬¡°>¡±´ú±í¸ü½Ó½ü×Öĸ±íβ²¿¡£Ò»°ãÀ´Ëµ£¬Ð¡Ð´×Öĸ´óÓÚ´óд×Öĸ£¬´óд×Öĸ´óÓÚÊý×Ö£¬µ«ÊÇÕâ¿ÉÄÜÒÀÀµÓÚ·þÎñÆ÷ÉϲÙ×÷ϵͳµÄ±È½Ï˳Ðò¡£
¡¡¡¡ÔڱȽÏʱ£¬Ä©Î²µÄ¿Õ¸ñÊDZ»ºöÂԵġ£ÀýÈ磬¡°Dirk¡±µÈÓÚ¡°Dirk ¡±¡£
¡¡¡¡ÔڱȽÏÈÕÆÚʱ£¬¡°<¡±±íʾÔçÓÚ£¬¡°>¡±±íʾÍíÓÚ¡£
¡¡¡¡ÔÚʹÓñȽÏÔËËã·û±È½ÏcharacterºÍdatetimeÊý¾Ýʱ£¬ÐèÓÃÒýºÅ½«ËùÓÐÊý¾ÝÒýÆðÀ´¡£
¡¡¡¡expression£º¿ÉÄÜÊÇÁÐÃû¡¢³£Êý¡¢º¯Êý»òÕßÊÇÁÐÃû»ò³£ÊýµÄÈÎÒâ×éºÏ£¬ÒÔ¼°ÒÔËãÊõÔËËã·û»òÖðλÔËËã·ûÁ¬½ÓµÄº¯Êý¡£ËãÊõÔËËã·ûÈçϱíËùʾ£º
ÒÔÏÂÊÇÒýÓÃƬ¶Î£º ·ûºÅ ÒâÒå + ¼ÓºÅ - ¼õºÅ * ³ËºÅ / ³ýºÅ
¡¡¡¡is null£ºÔÚËÑË÷Ò»¸öNULLֵʱʹÓá£
¡¡¡¡like£º¹Ø¼ü´Ê£¬¶Ôchar¡¢varcharºÍdatetime(²»°üÀ¨ÃëºÍºÁÃë)¿ÉÒÔʹÓÃlike£¬ÔÚMySQLÖÐlikeÒ²¿ÉÒÔÓÃÔÚÊý×ֵıí´ïʽÉÏ¡£
¡¡¡¡µ±Óû§ÔÚËÑË÷datetimeÐÍÊý¾Ýʱ£¬×îºÃÊÇʹÓùؼü´Êlike£¬ÒòΪÍêÕûµÄdatetime¼Ç¼°üº¬¸÷ÖÖ¸÷ÑùµÄÈÕÆÚ×é¼þ¡£ÀýÈçÓû§ÔÚÁÐarrival_timeÖмÓÈëÒ»¸öÖµ¡°9:20¡±£¬¶ø×Ó¾äwhere arrival_time=¡°9:20¡±È´Ã»Óз¢ÏÖËü£¬ÒòΪMySQL°Ñ¼ÈëµÄÊý¾Ýת»»³ÉÁË¡°Jan 1,1900 9:20AM¡±¡£È»¶ø×Ó¾äwhere arrival_time like¡°%9:20%¡±¾ÍÄÜÕÒµ½Ëü¡£
¡¡¡¡boolean_expression£º·µ»Ø¡°true¡±»ò¡°false¡±ÖµµÄ±í´ïʽ¡£
¡¡¡¡match_string£ºÓÉ×Ö·ûºÍͨÅä·û×é³ÉµÄ´®£¬Óõ¥ÒýºÅ»òË«ÒýºÅÒýÆðÀ´£¬ÊÇÆ¥Åäģʽ¡£Í¨Åä·ûÈçϱíËùʾ£º
ÒÔÏÂÊÇÒýÓÃƬ¶Î£º ·ûºÅ ÒâÒå % 0»ò¶à¸ö×Ö·ûµÄ×Ö·û´® _ ÈκÎÒ»µ¥¸ö×Ö·û not£º ·ñ¶¨ÈκÎÂß¼±í´ïʽ£¬»òÊǹؼü´Ê£¬ Èçlike£¬null£¬betweenµÈ¡£ group byºÍhaving×Ó¾äÔÚselectÓï¾äÖÐʹÓ㬠¿ÉÒÔ½«±í»®·Ö³É×é²¢·µ»ØÆ¥Åähaving×Ó¾äÌõ¼þµÄ×é¡£ Óï·¨£ºselectÓï¾ä¿ªÍ· group by [all] aggregate_free_expression [,aggregate_free_expression]* [having search_conditions]
¡¡¡¡selectÓï¾ä½áβ
¡¡¡¡group by£ºÖ¸¶¨±í½«»®·ÖµÄ×éȺ£¬Èç¹ûÔÚselect±íÏîÖаüº¬¼¯ºÏº¯Êý£¬ÔòΪ¸÷×é¼ÆËãÒ»¸ö×ܼÆÖµ¡£ÕâЩ×ܼÆÖµµÄ½á¹ûÒÔеÄÁÐÏÔʾ£¬¶ø²»ÊÇеÄÐС£ÔÚhaving×Ó¾äÖÐÓû§¿ÉÒÔÒýÓÃÕâЩеÄ×ܼÆÁС£ÔÚgroup by֮ǰµÄselect_listÖпÉÒÔʹÓÃavg¡¢count¡¢max¡¢minºÍsumµÈ¼¯ºÏº¯Êý¡£±í¿ÉÒÔ±»ÈÎÒâÁеÄ×éºÏ·Ö×é¡£
¡¡¡¡all£ºÔÚ½á¹ûÖаüº¬ËùÓÐ×éȺµÄTransact-SQLÀ©Õ¹£¬ÕâÀïµÄËùÓÐ×éȺÉõÖÁ°üÀ¨ÄÇЩ±»where×Ó¾äËùÅųýµÄ×éȺ¡£Èç¹ûͬʱʹÓÃhaving×Ӿ䣬½«¶ÔallµÄÒâÒå½øÐзñ¶¨¡£
¡¡¡¡aggregate_free_expression£º²»°üº¬¼¯ºÏº¯ÊýµÄ±í´ïʽ£¬Transact-SQLÀ©Õ¹ÔÊÐíÔÚÓÃÁÐÃû³Æ·Ö×éµÄͬʱ£¬ÓÃÎÞ¼¯ºÏº¯ÊýµÄ±í´ïʽ·Ö×é¡£
¡¡¡¡having£ºÎªgroup by×Ó¾äÉèÖÃÌõ¼þ£¬ÀàËÆÓÚwhereΪselectÓï¾äÉèÖÃÌõ¼þµÄ·½·¨¡£havingµÄ²éÕÒÌõ¼þ¿ÉÒÔ°üÀ¨¼¯ºÏº¯Êý±í´ïʽ¡£³ý´ËÖ®Í⣬ËüµÄ²éÕÒÌõ¼þÓëwhere²éÕÒÌõ¼þÏàͬ¡£
¡¡¡¡order by£º°´ÁÐÅÅÁнá¹û¡£¶ÔselectÊä³öµÄÁпÉÒÔÓÃÁÐÃû¡¢ÁбðÃû»òÁÐλÖÃÀ´ÒýÓá£ÀýÈ磺select id as myid,name as myname from mytable group by id¡¢select id as myid,name as myname from mytable group by myid¡¢select id as myid,name as myname from mytable group by 1ÕâÈý¾äÊÇÍêÈ«µÈ¼ÛµÄ¡£µ±È»£¬ÎÒÃDz»ÔÞ³ÉÓõÚÈýÖÖÓ÷¨£¬Õ⽫¸ø³ÌÐòµÄ¿É¶ÁÐÔ´øÀ´²»ºÃµÄÓ°Ïì¡£ ΪÁËÒÔ½µÐòÅÅÁУ¬°ÑDESC¹Ø¼ü´Ê¼Óµ½order by×Ó¾äÖÐÄãÒªÅÅÐòµÄÁÐÃûÇ°¡£È±Ê¡ÊÇÉýÐò£¬ÄãÒ²¿ÉÒÔÓÃASC¹Ø¼ü´ÊÃ÷È·Ö¸¶¨¡£
¡¡¡¡limit ×Ӿ䣺ÓÃÀ´ÏÞÖÆselectÓï¾ä·µ»ØµÄÐÐÊý¡£limitÈ¡1¸ö»ò2¸öÊý×Ö²ÎÊý£¬Èç¹û¸ø¶¨2¸ö²ÎÊý£¬µÚÒ»¸öÖ¸¶¨Òª·µ»ØµÄµÚÒ»ÐеÄÆ«ÒÆÁ¿£¬µÚ¶þ¸öÖ¸¶¨·µ»ØÐеÄ×î´óÊýÄ¿¡£³õʼÐеÄÆ«ÒÆÁ¿ÊÇ0(²»ÊÇ1)¡£Èç¹û¸ø¶¨Ò»¸ö²ÎÊý£¬ËüÖ¸³öÆ«ÒÆÁ¿Îª0µÄ·µ»ØÐеÄ×î´óÊýÄ¿¡£Ò²¾ÍÊÇ˵limit 5ºÍlimit 0,5ÍêÈ«µÈ¼Û¡£
¡¡¡¡ÖÁÓÚprocedure¹Ø¼ü´ÊµÄº¬Ò壬ÎÒҲû¸ãµÃÌ«Çå³þ£¬ºÃÏóÊǶԴ洢¹ý³ÌµÄÖ§³Ö£¬¶øMySQL±¾Éí²»Ö§³Ö´æ´¢¹ý³Ì£¬¿´À´ÊÇΪÁ˽«À´À©³äµÄÐèÒª¶ø±£ÁôµÄ°É¡£
ÎÄÕÂÀ´Ô´£ºÌ켫ÂÛ̳ÕûÀí |