ÔõÑùÓÃthinkphp mÒªÁìʵÏÖ¶à±íÅÌÎÊ
Ò»¡¢Ê²Ã´ÊǶà±íÅÌÎÊ
ÔÚÊý¾Ý¿âÖУ¬ÓÐʱÎÒÃÇÐèÒªÅÌÎʶà¸öÊý¾Ý±íÖ®¼äµÄÊý¾Ý¹Øϵ£¬Õâ¾ÍÐèҪʹÓöà±íÅÌÎÊ¡£Ò»Ñùƽ³£ÇéÐÎÏ£¬ÎÒÃÇ¿ÉÒÔʹÓöà¸öSELECTÓï¾äÀ´ÊµÏÖ¶à±íÅÌÎÊ£¬µ«ÕâÖÖ·½·¨ÔÚ´¦Àí´ó×ÚÊý¾Ýʱ»áºÜÂý£¬Òò´ËÎÒÃÇÐèҪʹÓøü¸ßЧµÄ·½·¨¾ÙÐжà±íÅÌÎÊ¡£
¶þ¡¢thinkphp MÒªÁì¶à±íÅÌÎʵÄʵÏÖ
ÔÚthinkphpÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃMÒªÁìÀ´ÊµÏÖ¶à±íÅÌÎÊ¡£MÒªÁìÊÇÒ»ÖÖ¸ßЧµÄORM£¨¹¤¾ß¹ØϵӳÉ䣩·½·¨£¬Ëü¿ÉÒÔ×ÊÖúÎÒÃÇ·â×°SQLÓï¾ä£¬´Ó¶ø¸üÀû±ãµØ¾ÙÐÐÊý¾Ý´¦Àí¡£
1.»ù´¡Ê¹ÓÃÒªÁ죺
ʹÓÃMÒªÁì¾ÙÐжà±íÅÌÎÊÊ®·Ö¼òÆÓ£¬ÎÒÃÇÖ»ÐèÒªÔÚʹÓÃMÒªÁìʱ´«Èë¶à¸ö±íÃû£¬¾Í¿ÉÒÔʵÏÖ¶à±íÅÌÎÊ¡£
ÀýÈçÎÒÃÇÓÐÁ½¸öÊý¾Ý±í£ºuserºÍorder£¬ÏÖÔÚÎÒÃÇÐèÒªÅÌÎÊuser±íºÍorder±íÖеÄÊý¾Ý¹Øϵ£¬ÎÒÃÇ¿ÉÒÔÕâÑù×ö£º
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
$user_order = M('User')->table(array('user'=>'u','order'=>'o'))->where('u.id = o.user_id')->select();
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃÇʹÓÃÁËtableÒªÁì²¢´«ÈëÁ˶à¸ö±íÃû£¬Í¨¹ýwhereÒªÁìÀ´Ö¸¶¨Á½¸öÊý¾Ý±íÖ®¼äµÄ¹Øϵ£¬×îºóʹÓÃselectÒªÁìÀ´»ñÈ¡ÅÌÎÊЧ¹û¡£
2.¶àÌõ¼þÅÌÎÊ£º
ÔÚ¾ÙÐжà±íÅÌÎÊʱ£¬ÎÒÃÇͨ³£ÐèÒªÖ¸¶¨¶à¸öÅÌÎÊÌõ¼þ£¬ÎÒÃÇ¿ÉÒÔʹÓÃÆ´½Ó×Ö·û´®µÄ·½·¨À´ÊµÏÖ¶àÌõ¼þÅÌÎÊ£¬Ò²¿ÉÒÔʹÓÃÊý×éµÄ·½·¨À´ÊµÏÖ¶àÌõ¼þÅÌÎÊ¡£
ÀýÈçÎÒÃÇÐèÒªÅÌÎÊuser±íÖÐÐÔ±ðΪÄÐÐÔ£¨gender=1£©µÄÓû§ºÍuser±íÖеĶ©µ¥ÐÅÏ¢£¬ÎÒÃÇ¿ÉÒÔÕâÑù×ö£º
$user_order = M('User')->table(array('user'=>'u','order'=>'o'))->where(array('u.gender'=>1,'u.id=o.user_id'))->select();
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃÇʹÓÃÁËwhereÒªÁì²¢´«ÈëÁËÅÌÎÊÌõ¼þÊý×飬¸ÃÊý×éÖаüÀ¨ÁËÁ½¸öÅÌÎÊÌõ¼þ£¬ÆäÖÐÒ»¸öÊÇÅÌÎÊÐÔ±ðΪÄÐÐÔµÄÓû§£¬ÁíÒ»¸öÊÇÅÌÎʶ©µ¥ÐÅÏ¢¡£
3.¶à±íÅÅÐò£º
ÔÚ¾ÙÐжà±íÅÌÎÊʱ£¬ÎÒÃÇÓÐʱÐèÒª¶ÔÅÌÎÊЧ¹û¾ÙÐÐÅÅÐò£¬ÎÒÃÇ¿ÉÒÔʹÓÃorderÒªÁìÀ´¾ÙÐжà±íÅÅÐò¡£
ÀýÈçÎÒÃÇÐèÒªÅÌÎÊuser±íÖеÄÓû§ºÍorder±íÖеĶ©µ¥£¬²¢Æ¾Ö¤¶©µ¥½ð¶î¾ÙÐÐÅÅÐò£¬ÎÒÃÇ¿ÉÒÔÕâÑù×ö£º
$user_order = M('User')->table(array('user'=>'u','order'=>'o'))->where('u.id=o.user_id')->order('o.amount desc')->select();
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃÇʹÓÃÁËorderÒªÁì²¢´«ÈëÁËÅÅÐòÌõ¼þ£¬¸ÃÌõ¼þÊÇƾ֤¶©µ¥½ð¶î¾ÙÐÐÅÅÐòµÄ¡£
ÒÔÉϾÍÊÇÔõÑùÓÃthinkphp mÒªÁìʵÏÖ¶à±íÅÌÎʵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡