thinkphpÔõÑù¾ÙÐжà±íÁ´½ÓÅÌÎÊ
Ҫѧ»áÔÚThinkPHPÖоÙÐжà±íÁ´½ÓÅÌÎÊ£¬Ê×ÏÈÐèÒªÕÆÎÕ»ù±¾Óï·¨¡£ÔÚThinkPHPÖУ¬¿ÉÒÔͨ¹ýÒÔÏ·½·¨¾ÙÐжà±íÁ´½ÓÅÌÎÊ£º
Db::table('table1') ->alias('t1') ->join('table2 t2', 't1.id = t2.table1_id') ->join('table3 t3', 't1.id = t3.table1_id') ->where('t1.id', $id) ->select();
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöÓï¾äÖУ¬ÎÒÃÇͨ¹ýjoinÒªÁì¾ÙÐÐÁ˶à±íÁ´½Ó£¬ÆäÖеÚÒ»¸ö²ÎÊýΪҪÁ´½ÓµÄ±íÃû£¬µÚ¶þ¸ö²ÎÊýΪÁ´½ÓÌõ¼þ¡£ÎÒÃÇ¿ÉÒÔʹÓÃÓÖÃûaliasÀ´Îª±íÌí¼ÓÓÖÃû£¬Àû±ãºóÐø²Ù×÷¡£
ÔÚwhereÒªÁìÖУ¬ÎÒÃÇ¿ÉÒÔÖ¸¶¨ÐèҪɸѡµÄÌõ¼þ¡£ÔÚ±¾ÀýÖУ¬ÎÒÃÇͨ¹ýt1.idÀ´É¸Ñ¡t1±íÖÐid×ֶμ´ÊÇ$idµÄ¼Í¼¡£
ÎÒÃÇ¿ÉÄÜÐèÒª¾ÙÐиüΪÖØ´óµÄÅþÁ¬ÅÌÎÊ£¬½«¶à¸ö±íÖеÄÊý¾ÝÕûºÏÔÚÒ»Æð¾ÙÐÐͳ¼ÆÆÊÎö¡£Õâʱ£¬ÎÒÃÇ¿ÉÒÔʹÓÃThinkPHPÌṩµÄ¾ÛºÏº¯ÊýÀ´¾ÙÐÐÅÌËã¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ÀýÈ磬ÎÒÃÇҪͳ¼Æÿ¸öÓû§µÄ¶©µ¥ÊýÄ¿£¬¿ÉÒÔʹÓÃÈçÏÂÓï¾ä¾ÙÐÐÅÌÎÊ£º
Db::table('user') ->alias('u') ->join('order o', 'u.id = o.user_id') ->field('u.*, COUNT(o.id) as order_num') ->group('u.id') ->select();
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöÓï¾äÖУ¬ÎÒÃÇʹÓÃÁËCOUNTº¯ÊýÀ´Í³¼Æorder±íÖÐÿ¸öÓû§µÄ¶©µ¥ÊýÄ¿£¬²¢Ê¹ÓÃgroupÒªÁ콫Ч¹ûƾ֤u.id¾ÙÐзÖ×é¡£
³ý´ËÖ®Í⣬ÎÒÃÇ»¹¿ÉÒÔʹÓÃHAVINGÌõ¼þ¾ÙÐиü½øÒ»²½µÄɸѡ¡£ÀýÈ磬ÎÒÃÇҪɸѡ³ö¶©µ¥ÊýÄ¿´óÓÚ¼´ÊÇ5µÄÓû§£¬¿ÉÒÔʹÓÃÈçÏÂÓï¾ä£º
Db::table('user') ->alias('u') ->join('order o', 'u.id = o.user_id') ->field('u.*, COUNT(o.id) as order_num') ->group('u.id') ->having('order_num >= 5') ->select();
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöÓï¾äÖУ¬ÎÒÃÇʹÓÃÁËHAVINGÌõ¼þÀ´É¸Ñ¡³ö¶©µ¥ÊýÄ¿´óÓÚ¼´ÊÇ5µÄÓû§¡£
ÒÔÉϾÍÊÇthinkphpÔõÑù¾ÙÐжà±íÁ´½ÓÅÌÎʵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡