laravelÅÌÎÊÌõ¼þ
laravel ÊÇÒ»¸ö»ùÓÚphpÓïÑÔ¿ª·¢µÄ¿ªÔ´ web Ó¦ÓóÌÐò¿ò¼Ü£¬ËüÓµÓмòÆÓ¡¢ÓÅÑŵÄÓï·¨ºÍÇ¿Ê¢µÄ¹¦Ð§£¬ÊÇÏÖ´ú web Ó¦ÓóÌÐò¿ª·¢µÄÏȽøÖ¸ÄÏ¡£ÆäÖÐÓÈΪÖ÷ÒªµÄÊÇËüÇ¿Ê¢µÄÅÌÎʹ¹½¨Æ÷£¬Ê¹µÃÎÒÃÇ¿ÉÒÔÇáËɵع¹½¨³öÖØ´óµÄ sql ÅÌÎÊÓï¾ä£¬¶ø²»ÐèÒªÖ±½Ó²Ù×÷ sql Óï¾ä¡£
ͨ¹ý±¾ÎĵÄѧϰ£¬Äã¿ÉÒÔÏàʶµ½ Laravel Öг£¼ûµÄÅÌÎÊÌõ¼þ¼°ÆäÓ÷¨¡£
»ù±¾µÄÅÌÎÊÒªÁì
Laravel ÌṩÁË select() ÒªÁìÀ´Ñ¡ÔñÐèÒªÅÌÎʵÄÁУ¬¿ÉÒÔÖ±½Óת´ïÁÐÃûµ½¸ÃÒªÁì»òÕßʹÓÃÊý×éÀ´×ª´ïÁÐÃû¡£from() ·½¹æÔòÖ¸¶¨±íÃû¡£ÀýÈ磬ÒÔÏ´úÂë¿ÉÒÔÅÌÎÊ users ±íÖÐµÄ id ºÍ name ÁУº
$users = DB::table('users') ->select('id', 'name') ->get();
µÇ¼ºó¸´ÖÆ
³ýÁË select() ÒªÁìÖ®Í⣬Laravel »¹ÌṩÁË where() ÒªÁìÀ´Ìí¼Ó WHERE Ô¼ÊøÌõ¼þ¡£ÀýÈ磬ÒÔÏ´úÂëÅÌÎÊ users ±íÖÐËùÓÐÐÕ “Smith” µÄÈË£º
$users = DB::table('users') ->where('name', 'Smith') ->get();
µÇ¼ºó¸´ÖÆ
OR Ìõ¼þÅÌÎÊ
ÓÐʱ¼ä£¬ÔÚÅÌÎÊÊý¾ÝʱÎÒÃÇÐèҪʹÓà OR ÔËËã·ûÀ´ÅþÁ¬¶à¸öÌõ¼þ£¬ÅÌÎʶà¸öÌõ¼þÖÐÊʺϵÄÐС£Laravel ÖпÉÒÔʹÓà orWhere() ÒªÁìÀ´ÊµÏÖ£¬Ê¾ÀýÈçÏ£º
$users = DB::table('users') ->where('name', 'Smith') ->orWhere('name', 'Johnson') ->get();
µÇ¼ºó¸´ÖÆ
ÉÏÊöÓï¾ä½«»áÏÈÅÌÎÊÐÕ “Smith” µÄËùÓÐÈË£¬È»ºóÅÌÎÊÐÕ “Johnson” µÄËùÓÐÈË£¬²¢½«ÕâÁ½¸öÅÌÎÊЧ¹ûÈ¡²¢¼¯ºó·µ»Ø¡£
IN Ìõ¼þÅÌÎÊ
ÔÚ Laravel ÖУ¬¿ÉÒÔʹÓà whereIn() ÒªÁìʵÏÖ IN Ìõ¼þÅÌÎÊ¡£ÒÔÏÂÊÇÒ»¸öʾÀý£¬Ëü½«ÅÌÎÊ users ±íÖÐËùÓÐÒÔ w¡¢x¡¢y¡¢z ×Öĸ´òÍ·µÄÃû×Ö£º
$users = DB::table('users') ->whereIn('name', ['W%', 'X%', 'Y%', 'Z%']) ->get();
µÇ¼ºó¸´ÖÆ
BETWEEN Ìõ¼þÅÌÎÊ
Laravel »¹ÌṩÁË whereBetween() ÒªÁ죬¸ÃÒªÁìÓÃÓÚÅÌÎÊij¸ö×ֶεÄÊýÖµÊÇ·ñÔÚÖ¸¶¨µÄ¹æÄ£ÄÚ¡£
ÒÔÏ´úÂ뽫ÅÌÎÊ users ±íÖÐ age ÁÐÔÚ 18 µ½ 30 Ö®¼äµÄÈË£º
$users = DB::table('users') ->whereBetween('age', [18, 30]) ->get();
µÇ¼ºó¸´ÖÆ
³ýÁËÉÏÊöʾÆÆÀý£¬Laravel »¹ÌṩÁËÆäËûһЩÅÌÎÊÒªÁ죬ÀýÈç whereNotBetween(), groupBy(), orderBy() µÈµÈ£¬ÕâЩҪÁì¿ÉÒÔ×ÊÖúÎÒÃÇÔ½·¢ÇáËɵع¹½¨ÖØ´óµÄ SQL ÅÌÎÊÓï¾ä¡£
×ÜÖ®£¬Laravel ÌṩÁËÐí¶àÅÌÎÊÌõ¼þ£¬Àû±ã¿ª·¢Õß¹¹½¨Æ¾Ö¤ÐèÇóÅÌÎÊÊý¾ÝµÄ¹¦Ð§¡£ÎÒÃÇÖ»ÐèÒªÊìÁ·ÕÆÎÕÕâЩҪÁ첢׼ȷʹÓ㬾ÍÄܹ»¿ìËٵع¹½¨³öÖª×ãÐèÇóµÄÓ¦ÓóÌÐò¡£
ÒÔÉϾÍÊÇlaravelÅÌÎÊÌõ¼þµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡