thinkphp¿ò¼ÜÔõÑùÅÌÎÊÊý¾Ý
Ò»¡¢»ù±¾ÅÌÎÊ
ÔÚThinkPHP¿ò¼ÜÖУ¬¿ÉÒÔʹÓÃÄ£×ÓÀàµÄfind¡¢select¡¢where¡¢orderµÈÒªÁì¾ÙÐлù±¾µÄÅÌÎʲÙ×÷¡£
£¨1£©findÒªÁì
findÒªÁìÓÃÓÚÅÌÎÊÒ»ÌõÊý¾Ý¼Í¼£¬¿ÉÒÔÖ±½Óƾ֤Ö÷¼ü¾ÙÐÐÅÌÎÊ¡£ÀýÈ磺
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
$user = UserModel::find(1);
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂ뽫´ÓUserModelÄ£×ÓÀàÖÐÅÌÎÊIDΪ1µÄÓû§¼Í¼¡£
£¨2£©selectÒªÁì
selectÒªÁì¿ÉÓÃÓÚ¼ìË÷¶àÌõÊý¾Ý¼Í¼£¬²¢¿ÉʹÓÃwhereºÍorderµÈÒªÁì¶ÔÆä¾ÙÐÐÏÞÖƺÍÅÅÐò¡£ÀýÈ磺
$list = UserModel::where('age', '>', 18)->order('id desc')->select();
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂ뽫´ÓUserModelÄ£×ÓÀàÖÐÅÌÎÊÄêËê´óÓÚ18ËêµÄËùÓÐÓû§¼Í¼£¬²¢Æ¾Ö¤IDµ¹ÐòÅÅÐò¡£
£¨3£©whereÒªÁì
whereÒªÁìÓÃÓÚÉèÖÃÌõ¼þ£¬¿ÉÒÔÓÃÓÚ»ù±¾ÅÌÎʺÍͳ¼ÆÅÌÎÊ¡£ÀýÈ磺
$count = UserModel::where('gender', 1)->count();
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂ뽫ͳ¼ÆUserModelÄ£×ÓÀàÖÐÐÔ±ðΪÄеÄÓû§¸öÊý¡£
£¨4£©orderÒªÁì
orderÒªÁìÓÃÓÚÉèÖÃÅÅÐò¡£ÀýÈ磺
$list = UserModel::order('id desc')->select();
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂ뽫´ÓUserModelÄ£×ÓÀàÖÐÅÌÎÊËùÓÐÓû§¼Í¼£¬²¢Æ¾Ö¤IDµ¹ÐòÅÅÐò¡£
¶þ¡¢¸ß¼¶ÅÌÎÊ
³ýÁË»ù±¾µÄÅÌÎʲÙ×÷£¬ThinkPHP¿ò¼Ü»¹ÌṩÁËÇ¿Ê¢µÄ¸ß¼¶ÅÌÎʹ¦Ð§£¬°üÀ¨¶à±íÅÌÎÊ¡¢Á¬±íÅÌÎÊ¡¢Í³¼ÆÅÌÎʵȡ£
£¨1£©¶à±íÅÌÎÊ
ÔÚ¶à±íÅÌÎÊʱ£¬ÐèҪʹÓöà¸öÄ£×ÓÀà¡£ÀýÈ磺
$list = UserModel::alias('u')->join('order o', 'u.id = o.user_id')->where('u.age', '>', 18)->select();
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂ뽫´ÓUserModelºÍOrderModelÄ£×ÓÀàÖÐÅÌÎÊÄêËê´óÓÚ18ËêµÄËùÓÐÓû§ºÍËûÃǵĶ©µ¥¼Í¼¡£
£¨2£©Á¬±íÅÌÎÊ
Á¬±íÅÌÎÊÊǶà±íÅÌÎʵÄÆäÖÐÒ»ÖÖÐÎʽ£¬Ê¹ÓÃThinkPHPµÄjoinÒªÁì²Ù×÷£º
$list = UserModel::join('order o', 'user.id = o.user_id')->select();
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂ뽫´ÓUserModelºÍOrderModelÄ£×ÓÀàÖÐÅÌÎÊËùÓÐÓû§ºÍËûÃǵĶ©µ¥¼Í¼¡£
£¨3£©Í³¼ÆÅÌÎÊ
ÔÚͳ¼ÆÅÌÎÊʱ£¬¿ÉÒÔʹÓÃÄ£×ÓÀàµÄcountÒªÁì¾ÙÐвÙ×÷£¬ÀýÈ磺
$count = UserModel::where('age', '>', 18)->count();
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂ뽫ͳ¼ÆUserModelÄ£×ÓÀàÖÐÄêËê´óÓÚ18ËêµÄÓû§ÊýÄ¿¡£
ÒÔÉϾÍÊÇthinkphp¿ò¼ÜÔõÑùÅÌÎÊÊý¾ÝµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡