ThinkPHPÖеıíÅÌÎÊÓï¾äÔõÑùʹÓÃ
Ò»¡¢ÅÌÎʵ¥¸ö×Ö¶Î
ÒªÅÌÎʱíÖеÄij¸ö×ֶΣ¬ÎÒÃÇ¿ÉÒÔͨ¹ý table() º¯ÊýÀ´Ö¸¶¨ÒªÅÌÎÊµÄ±í£¬¶ø find() º¯ÊýÔòÌåÏÖÖ»ÅÌÎÊÒ»ÌõЧ¹û£¬ÀàËÆÓÚ MySQL ÖÐµÄ SELECT * FROM table_name WHERE column_name = ‘value’ LIMIT 1¡£
ʾÀý´úÂ룺
$data = Db::table('user')->where('id', 1)->value('username'); echo $data; // Êä³öЧ¹û£º'ÕÅÈý'
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃÇʹÓà table() º¯ÊýÖ¸¶¨ÒªÅÌÎʵıíΪ user£¬ÔÙʹÓà where() º¯ÊýÀ´Ö¸¶¨ÅÌÎÊÌõ¼þ£¬ÕâÀïÎÒÃÇÖ»ÅÌÎÊ id Ϊ 1 µÄ¼Í¼£¬²¢Ê¹Óà value() º¯ÊýÅÌÎÊ username ×ֶεÄÖµ¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
¶þ¡¢ÅÌÎÊÕûÌõ¼Í¼
Òª»ñÈ¡±íÖеÄÍêÕû¼Í¼£¬¿ÉʹÓà find() »ò select() ÒªÁì¡£ÆäÖУ¬find() º¯ÊýÌåÏÖÅÌÎʵÚÒ»ÌõÖª×ãÌõ¼þµÄ¼Í¼£¬¶ø select() º¯ÊýÔòÌåÏÖÅÌÎÊËùÓÐÖª×ãÌõ¼þµÄ¼Í¼¡£
ʾÀý´úÂ룺
$data = Db::table('user')->where('username', 'ÕÅÈý')->find(); print_r($data); // Êä³öЧ¹û£º['id' => 1, 'username' => 'ÕÅÈý', 'age' => 25]
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃÇÏÈʹÓà table() º¯ÊýÖ¸¶¨ÒªÅÌÎʵıíΪ user£¬ÔÙʹÓà where() º¯ÊýÖ¸¶¨ÅÌÎÊÌõ¼þ£¬ÅÌÎÊ username Ϊ ‘ÕÅÈý’ µÄ¼Í¼£¬×îºóʹÓà find() º¯ÊýÅÌÎÊÕûÌõ¼Í¼¡£
Èý¡¢ÅÌÎʶà¸ö×Ö¶Î
ÈôÊÇÒªÅÌÎʱíÖеĶà¸ö×ֶΣ¬ÎÒÃÇ¿ÉÒÔʹÓà field() º¯ÊýÀ´Ö¸¶¨ÒªÅÌÎʵÄ×ֶΣ¬¶à¸ö×Ö¶ÎÖ®¼äÓöººÅÀë¸ô¡£
ʾÀý´úÂ룺
$data = Db::table('user')->where('id', 1)->field('username, age')->find(); print_r($data); // Êä³öЧ¹û£º['username' => 'ÕÅÈý', 'age' => 25]
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃÇʹÓà field() º¯ÊýÖ¸¶¨ÒªÅÌÎʵÄ×Ö¶ÎΪ username ºÍ age£¬ÅÌÎÊ id Ϊ 1 µÄ¼Í¼£¬×îºóʹÓà find() º¯ÊýÅÌÎÊÕûÌõ¼Í¼¡£
ËÄ¡¢ÅÌÎʶàÌõ¼Í¼
ʹÓà select() º¯Êý¿ÉÅÌÎʱíÖÐÇкÏÅÌÎÊÌõ¼þµÄ¶à¸ö¼Í¼¡£Óëfind()º¯ÊýµÄ²î±ðÖ®´¦ÔÚÓÚ£¬select()º¯Êý·µ»ØÓÉЧ¹û¼¯Êý×é×é³ÉµÄ¶þάÊý×é¡£
ʾÀý´úÂ룺
$data = Db::table('user')->where('age', '>', 20)->select(); print_r($data); // Êä³öЧ¹û£º[['id' => 1, 'username' => 'ÕÅÈý', 'age' => 25], ['id' => 2, 'username' => 'ÀîËÄ', 'age' => 28]]
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃÇʹÓà table() º¯ÊýÖ¸¶¨ÒªÅÌÎʵıíΪ user£¬ÔÙʹÓà where() º¯ÊýÖ¸¶¨ÅÌÎÊÌõ¼þ£¬ÅÌÎÊÄêËê´óÓÚ 20 µÄ¼Í¼£¬²¢Ê¹Óà select() º¯ÊýÅÌÎʶàÌõ¼Í¼¡£
Îå¡¢ÅÅÐòÅÌÎÊ
ÈôÊÇÒª¶ÔÅÌÎÊЧ¹û¾ÙÐÐÅÅÐò£¬ÎÒÃÇ¿ÉÒÔʹÓà order() º¯Êý£¬ÆäÖвÎÊý¿ÉÒÔÊÇ asc ÌåÏÖÉýÐò£¬Ò²¿ÉÒÔÊÇ desc ÌåÏÖ½µÐò¡£
ʾÀý´úÂ룺
$data = Db::table('user')->where('age', '>', 20)->order('age desc')->select(); print_r($data); // Êä³öЧ¹û£º[['id' => 2, 'username' => 'ÀîËÄ', 'age' => 28], ['id' => 1, 'username' => 'ÕÅÈý', 'age' => 25]]
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃÇʹÓà where() º¯ÊýÖ¸¶¨ÅÌÎÊÌõ¼þ£¬ÅÌÎÊÄêËê´óÓÚ 20 µÄ¼Í¼£¬²¢Ê¹Óà order() º¯Êý½«Ð§¹ûƾ֤ age ½µÐòÅÅÁУ¬×îºóʹÓà select() º¯ÊýÅÌÎʶàÌõ¼Í¼¡£
Áù¡¢·ÖÒ³ÅÌÎÊ
ÈôÊÇÅÌÎÊЧ¹ûÓÐÐí¶àÌõ¼Í¼£¬ÎÒÃÇ¿ÉÒÔʹÓà limit() º¯Êý¾ÙÐзÖÒ³ÅÌÎÊ£¬ÆäÖеÚÒ»¸ö²ÎÊýÌåÏּͼ×îÏȵÄλÖ㬵ڶþ¸ö²ÎÊýÌåÏÖÅÌÎʵļͼÊý¡£
ʾÀý´úÂ룺
$data = Db::table('user')->where('age', '>', 20)->order('age desc')->limit(0, 1)->select(); print_r($data); // Êä³öЧ¹û£º[['id' => 2, 'username' => 'ÀîËÄ', 'age' => 28]]
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃÇʹÓà where() º¯ÊýÖ¸¶¨ÅÌÎÊÌõ¼þ£¬ÅÌÎÊÄêËê´óÓÚ 20 µÄ¼Í¼£¬²¢Ê¹Óà order() º¯Êý½«Ð§¹ûƾ֤ age ½µÐòÅÅÁУ¬ÔÙʹÓà limit() º¯ÊýÅÌÎʵÚÒ»Ìõ¼Í¼¡£
ÒÔÉϾÍÊÇThinkPHPÖеıíÅÌÎÊÓï¾äÔõÑùʹÓõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡