ThinkPHPÖÐÔõôÅÌÎÊijЩ×Ö¶Î
ÔÚThinkPHPÖУ¬ÅÌÎÊijЩ×ֶκÜÊǼòÆÓ¡£Ê×ÏÈ£¬ÎÒÃÇÐèҪʹÓÃModelÀàÖеÄfield()ÒªÁì¡£´ËÒªÁìÔÊÐíÎÒÃÇÖ¸¶¨ÒªÅÌÎʵÄ×ֶΡ£¾ÙÀý˵£¬¼ÙÉèÎÒÃÇÓÐÒ»¸öÃûΪusersµÄÊý¾Ý±í£¬ÆäÖаüÀ¨id¡¢nameºÍemailµÈ¶à¸ö×ֶΡ£ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂëÀ´ÅÌÎÊÓû§µÄÐÕÃûºÍµç×ÓÓʼþ£º
$users = M('users')->field('name,email')->select();
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬M()ÒªÁìÓÃÓÚ»ñÈ¡Ò»¸öÄ£×Ó¹¤¾ß¡£ÎÒÃÇ¿ÉÒÔʹÓøÃÄ£×Ó¹¤¾ßÖ´ÐÐÅÌÎÊ¡£Ö¸¶¨ÅÌÎÊ×ֶοÉʹÓÃfield()ÒªÁ죬¶øÏÖʵִÐÐÅÌÎÊÔòӦŲÓÃselect()ÒªÁì¡£
ÈôÊÇÎÒÃÇÏëÒªÅÌÎʱí¸ñÖеÄËùÓÐ×ֶΣ¬ÎÒÃÇ¿ÉÒÔʹÓÃ*ͨÅä·û¡£ÀýÈ磺
$users = M('users')->select();
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇûÓÐʹÓÃfield()ÒªÁ죬¶øÊÇʹÓÃÁËͨÅä·û*¡£Õ⽫ѡÔñËùÓÐ×ֶΡ£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ÔÚÓÐЩÇéÐÎÏ£¬ÎÒÃÇ¿ÉÄÜÖ»ÏëÅÌÎʱí¸ñÖв¿·ÖÊý¾Ý¡£ThinkPHPÖÐÌṩÁËÒ»ÖÖÀû±ãµÄÒªÁìlimit()¡£¸ÃÒªÁìÔÊÐíÎÒÃÇÖ¸¶¨×î´ó·µ»Ø¼Í¼ÊýºÍ·µ»Ø¼Í¼µÄÆ«ÒÆÁ¿¡£ÎÒÃÇ¿ÉÒÔÕâÑùʹÓÃlimit()ÒªÁ죺
$users = M('users')->limit(10,20)->select();
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬Ê¹ÓÃlimit()ÒªÁìÀ´É趨´ÓµÚ11¸ö¼Í¼×îÏÈ·µ»Ø20¸ö¼Í¼¡£Õâ¹ØÓÚ·ÖÒ³ºÍ´óÐÍÊý¾Ý¼¯ºÜÊÇÓÐÓá£
³ýÁËʹÓÃfield()ºÍlimit()ÒªÁ죬ÎÒÃÇ»¹Äܹ»ÓÃjoin()ÒªÁì¡£¸ÃÒªÁìÔÊÐíÎÒÃǼÓÈëÆäËûÊý¾Ý±íÖеÄÊý¾Ý¡£ÀýÈ磬ÈôÊÇÎÒÃÇÓÐÒ»¸öÃûΪordersµÄ±í¸ñ£¬ÆäÖаüÀ¨Óû§µÄ¶©µ¥Êý¾Ý²¢ÇÒÓëusers±í¸ñͨ¹ýÓû§ID¹ØÁª£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂëÅÌÎÊÓû§µÄ¶©µ¥Êý¾Ý£º
$orders = M('orders')->join('users on users.id = orders.user_id')->select();
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬join()ÒªÁìʹÓÃÁËMySQL joinÓï¾ä£¬ÆäÖÐusersºÍorders±íͨ¹ýÓû§ID¹ØÁªÆðÀ´¡£
×îºó£¬ÎÒÃÇ¿ÉÒÔʹÓÃwhere()ÒªÁìÖ¸¶¨ÅÌÎÊÌõ¼þ¡£ÒÔÏ´úÂë¿ÉÒÔÓÃÀ´ÅÌÎÊÖ»ÓÐÓʼþ´øÓÐ@google.comµÄÓû§Êý¾Ý
$users = M('users')->field('name,email')->where('email like "%@google.com"')->select();
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬where()ÒªÁìÖ¸¶¨ÁËÅÌÎÊÌõ¼þ£ºÖ»Óеç×ÓÓʼþ×Ö¶ÎÒÔ@google.com×îºóµÄ¼Í¼²Å»á±»·µ»Ø¡£
ÒÔÉϾÍÊÇThinkPHPÖÐÔõôÅÌÎÊijЩ×ֶεÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡