Thinkphp5.0¶ÔÊý¾Ý¿âµÄ²Ù×÷ÒªÁìÓÐÄÄЩ
»ù±¾Ê¹ÓÃ
ÅÌÎʲÙ×÷
Db::query('select * from think_user where id=?',[8]);
µÇ¼ºó¸´ÖÆ
дÈë²Ù×÷
Db::execute('insert into think_user (id, name) values (?, ?)',[8,'thinkphp']);
µÇ¼ºó¸´ÖÆ
ÅÌÎʽṹÆ÷
ÅÌÎÊÊý¾Ý
find ·µ»ØÒ»Ìõ¼Í¼£¬·µ»ØµÄЧ¹ûÊÇÒ»¸öһάÊý×é ÈôÊÇЧ¹û²»±£´æ£¬·µ»ØNULL
Db::table('think_user')->where('id',1)->find();
µÇ¼ºó¸´ÖÆ
select ·µ»ØËùÓмͼ£¬·µ»ØµÄЧ¹ûÊÇÒ»¸ö¶þάÊý×é ÈôÊÇЧ¹û²»±£´æ£¬·µ»ØÒ»¸ö¿ÕÊý×é
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
Db::table('think_user')->where('status',1)->select();
µÇ¼ºó¸´ÖÆ
ÖúÊÖº¯Êý
ϵͳÌṩÁËÒ»¸ödbÖúÊÖº¯Êý£¬¿ÉÒÔ¸üÀû±ãµÄÅÌÎÊ£º
db('user')->where('id',1)->find(); db('user')->where('status',1)->select();
µÇ¼ºó¸´ÖÆ
×¢ÖØ£ºÊ¹ÓÃdbÖúÊÖº¯ÊýĬÈÏÿ´Î¶¼»áÖØÐÂÅþÁ¬Êý¾Ý¿â£¬¶øʹÓÃDb::name»òÕßDb::tableÒªÁìµÄ»°¶¼Êǵ¥ÀýµÄ¡£dbº¯ÊýÈôÊÇÐèÒª½ÓÄÉÏàͬµÄÁ´½Ó£¬¿ÉÒÔ´«ÈëµÚÈý¸ö²ÎÊý£¬ÀýÈç
db('user',[],false)->where('id',1)->find(); db('user',[],false)->where('status',1)->select();
µÇ¼ºó¸´ÖÆ
Ìí¼ÓÊý¾Ý
ʹÓà Db ÀàµÄ insert ÒªÁìÏòÊý¾Ý¿âÌá½»Êý¾Ý
$data = ['foo' => 'bar', 'bar' => 'foo']; Db::table('think_user')->insert($data);
µÇ¼ºó¸´ÖÆ
Ìí¼Ó¶àÌõÊý¾Ý
Ìí¼Ó¶àÌõÊý¾ÝÖ±½ÓÏò Db ÀàµÄ insertAll ÒªÁì´«ÈëÐèÒªÌí¼ÓµÄÊý¾Ý¼´¿É
$data = [ ['foo' => 'bar', 'bar' => 'foo'], ['foo' => 'bar1', 'bar' => 'foo1'], ['foo' => 'bar2', 'bar' => 'foo2'] ]; Db::name('user')->insertAll($data);
µÇ¼ºó¸´ÖÆ
insertAll ÒªÁìÌí¼ÓÊý¾ÝÀֳɷµ»ØÌí¼ÓÀֳɵÄÌõÊý
ÖúÊÖº¯Êý
// Ìí¼Óµ¥ÌõÊý¾Ý db('user')->insert($data); // Ìí¼Ó¶àÌõÊý¾Ý db('user')->insertAll($list);
µÇ¼ºó¸´ÖÆ
¸üÐÂÊý¾Ý
update ÒªÁì·µ»ØÓ°ÏìÊý¾ÝµÄÌõÊý£¬Ã»ÐÞ¸ÄÈκÎÊý¾Ý·µ»Ø 0
Db::table('think_user')->where('id', 1)->update(['name' => 'thinkphp']);
µÇ¼ºó¸´ÖÆ
¸üÐÂij¸ö×ֶεÄÖµ£º setField ÒªÁì·µ»ØÓ°ÏìÊý¾ÝµÄÌõÊý£¬Ã»ÐÞ¸ÄÈκÎÊý¾Ý×ֶηµ»Ø 0
Db::table('think_user')->where('id',1)->setField('name', 'thinkphp');
µÇ¼ºó¸´ÖÆ
×ÔÔö»ò×Ô¼õÒ»¸ö×ֶεÄÖµ // score ×Ö¶Î¼Ó 1 Db::table('think_user')->where('id', 1)->setInc('score'); // score ×Ö¶Î¼Ó 5 Db::table('think_user')->where('id', 1)->setInc('score', 5); // score ×ֶμõ 1 Db::table('think_user')->where('id', 1)->setDec('score'); // score ×ֶμõ 5 Db::table('think_user')->where('id', 1)->setDec('score', 5);
µÇ¼ºó¸´ÖÆ
ÑÓ³Ù¸üÐÂ
Db::table('think_user')->where('id', 1)->setInc('score', 1, 10);
µÇ¼ºó¸´ÖÆ
ɾ³ýÊý¾Ý
// ƾ֤Ö÷¼üɾ³ý Db::table('think_user')->delete(1); Db::table('think_user')->delete([1,2,3]); // Ìõ¼þɾ³ý Db::table('think_user')->where('id',1)->delete(); Db::table('think_user')->where('id','<',10)->delete();
µÇ¼ºó¸´ÖÆ
Ìõ¼þÅÌÎÊÒªÁì
where
¿ÉÒÔʹÓÃwhereÒªÁì¾ÙÐÐANDÌõ¼þÅÌÎÊ£º
Db::table('think_user') ->where('name','like','%thinkphp') ->where('status',1) ->find();
µÇ¼ºó¸´ÖÆ
whereOrÒªÁì
Db::table('think_user') ->where('name','like','%thinkphp') ->whereOr('title','like','%thinkphp') ->find();
µÇ¼ºó¸´ÖÆ
»ìÏýÅÌÎÊ
whereÒªÁìºÍwhereOrÒªÁìÔÚÖØ´óµÄÅÌÎÊÌõ¼þÖо³£ÐèÒªÅäºÏÒ»Æð»ìÏýʹÓã¬ÏÂÃæ¾Ù¸öÀý×Ó£º
$result = Db::table('think_user')->where(function ($query) { $query->where('id', 1)->whereor('id', 2); })->whereOr(function ($query) { $query->where('name', 'like', 'think')->whereOr('name', 'like', 'thinkphp'); })->select();
µÇ¼ºó¸´ÖÆ
ÅÌÎʱí´ïʽ
ÅÌÎʱí´ïʽ֧³Ö´ó²¿·ÖµÄSQLÅÌÎÊÓï·¨£¬Ò²ÊÇThinkPHPÅÌÎÊÓïÑԵľ«Ë裬ÅÌÎʱí´ïʽµÄʹÓÃÃûÌãº
where('×Ö¶ÎÃû','±í´ïʽ','ÅÌÎÊÌõ¼þ'); whereOr('×Ö¶ÎÃû','±í´ïʽ','ÅÌÎÊÌõ¼þ');
µÇ¼ºó¸´ÖÆ
±í´ïʽ ¼ÄÒå EQ¡¢= ¼´ÊÇ£¨=£© NEQ¡¢<> ²»¼´ÊÇ£¨<>£© GT¡¢> ´óÓÚ£¨>£© EGT¡¢>= ´óÓÚ¼´ÊÇ£¨>=£© LT¡¢< СÓÚ£¨<£© ELT¡¢<= СÓÚ¼´ÊÇ£¨<=£© LIKE Ä£ºýÅÌÎÊ [NOT] BETWEEN £¨²»ÔÚ£©Çø¼äÅÌÎÊ [NOT] IN £¨²»ÔÚ£©IN ÅÌÎÊ [NOT] NULL ÅÌÎÊ×Ö¶ÎÊÇ·ñ£¨²»£©ÊÇNULL [NOT] EXISTS EXISTSÅÌÎÊ EXP ±í´ïʽÅÌÎÊ£¬Ö§³ÖSQLÓï·¨ > time ʱ¼ä½ÏÁ¿ < time ʱ¼ä½ÏÁ¿ between time ʱ¼ä½ÏÁ¿ notbetween time ʱ¼ä½ÏÁ¿
µÇ¼ºó¸´ÖÆ
ÒÔÉϾÍÊÇThinkphp5.0¶ÔÊý¾Ý¿âµÄ²Ù×÷ÒªÁìÓÐÄÄЩµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡