thinkphp5ÔõÑùÈ¥³ýÖظ´ÅÌÎÊ
Ò»¡¢Ê¹Óà SELECT DISTINCT Òªº¦×Ö
ÔÚ SQL Óï¾äÖУ¬¿ÉÒÔʹÓà SELECT DISTINCT Òªº¦×ÖÀ´»ñÈ¡²»Öظ´µÄÊý¾Ý¡£ÔÚ ThinkPHP5 ÖУ¬Ê¹Óà \think\Db ÀàµÄ query() ÒªÁìÖ´ÐÐ SQL ÅÌÎÊÓï¾äʱ£¬¿ÉÒÔÖ±½ÓÔÚÅÌÎÊÌõ¼þÖÐʹÓà SELECT DISTINCT Òªº¦×Ö£¬ÀýÈ磺
use \think\Db; $data = Db::query('SELECT DISTINCT `brand` FROM `goods` WHERE `category_id` = 1');
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇͨ¹ý SELECT DISTINCT Òªº¦×Ö»ñÈ¡ÁË goods ±íÖÐ category_id Ϊ 1 µÄËùÓÐÆ·ÅÆ£¬²¢½«ÅÌÎÊЧ¹û¸³Öµ¸øÁË $data ±äÁ¿¡£
¶þ¡¢Ê¹Óà distinct() ÒªÁì
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
³ýÁËÔÚ SELECT Óï¾äÖÐʹÓà SELECT DISTINCT Òªº¦×ÖÍ⣬»¹¿ÉÒÔʹÓà ThinkPHP5 ÌṩµÄ distinct() ÒªÁìʵÏÖÈ¥ÖØÅÌÎÊ¡£ÈçÏÂËùʾ£º
use \think\Db; $data = Db::name('goods') ->where('category_id', 1) ->distinct(true) ->field('brand') ->select();
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇʹÓÃÁË Db ÀàµÄ name() ÒªÁìÖ¸¶¨ÁËÅÌÎʵÄÊý¾Ý±í£¬Ê¹ÓÃÁË where() ÒªÁìÖ¸¶¨ÁËɸѡÌõ¼þ£¬Ê¹ÓÃÁË distinct() ÒªÁ쿪ÆôÁËÈ¥ÖØÅÌÎÊ£¬Ê¹ÓÃÁË field() ÒªÁìÖ¸¶¨ÅÌÎʵÄ×ֶΣ¬×îºóʹÓÃÁË select() ÒªÁìÖ´ÐÐÅÌÎʲÙ×÷¡£
Èý¡¢Ê¹Óà group() ÒªÁì
³ýÁËʹÓà SELECT DISTINCT Òªº¦×ÖºÍ distinct() ÒªÁìÍ⣬»¹¿ÉÒÔʹÓà group() ÒªÁìʵÏÖÈ¥ÖØÅÌÎÊ¡£ÈçÏÂËùʾ£º
use \think\Db; $data = Db::name('goods') ->where('category_id', 1) ->group('brand') ->select();
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇʹÓÃÁË Db ÀàµÄ name() ÒªÁìÖ¸¶¨ÁËÅÌÎʵÄÊý¾Ý±í£¬Ê¹ÓÃÁË where() ÒªÁìÖ¸¶¨ÁËɸѡÌõ¼þ£¬Ê¹ÓÃÁË group() ÒªÁì¾ÙÐзÖ×飬×îºóʹÓÃÁË select() ÒªÁìÖ´ÐÐÅÌÎʲÙ×÷¡£
ÔÚÏÖʵµÄ¿ª·¢ÊÂÇéÖУ¬ÎÒÃÇÐèҪƾ֤ÏêϸµÄÓªÒµÐèÇóÑ¡ÔñºÏÊʵÄÈ¥ÖØÅÌÎÊÒªÁ죬ÒÔ°ü¹ÜÅÌÎÊЧ¹ûµÄ׼ȷÐÔºÍЧÂÊ¡£
ÒÔÉϾÍÊÇthinkphp5ÔõÑùÈ¥³ýÖظ´ÅÌÎʵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡