Home  >  Article  >  Backend Development  >  php mysql coherent operation

php mysql coherent operation

不言
不言Original
2018-04-13 17:42:341633browse

The content shared with you in this article is the coherent operation of php mysql, which has a certain reference value. Friends in need can refer to it

<?php
namespace app\index\controller;


use think\Db;

class User
{
    public function index()
    {
        // 一般的where方法进行字符串参数传入查询
//        dump(Db::name(&#39;user&#39;)
//            ->where(&#39;id=1 and name="wangjialin"&#39;)
//            ->find());

        // 参数为数组
        $map = [
//            &#39;id&#39;=> 2,
//            &#39;name&#39; => &#39;zhangsan&#39;,
//            &#39;age&#39; => [&#39;>&#39; , 10 ],
//            &#39;name&#39; => [&#39;like&#39; , &#39;%zhang%&#39;],
            &#39;age&#39; => [&#39;between&#39; , &#39;14,18&#39;]
        ];
//        dump(Db::name(&#39;user&#39;)->where($map)->select());

        // field order limit
//        dump(Db::name(&#39;user&#39;)
//            ->field(&#39;id , name  as username, age&#39;)
//            ->select());
//        dump(Db::name(&#39;user&#39;)
//            ->field([&#39;id&#39; , &#39;name as username&#39; , &#39;age&#39; , &#39;sex&#39;])
//            ->order(&#39;id desc&#39;)
//            ->limit(1)
//            ->select());

        // group having
//        dump(Db::name(&#39;user&#39;)
//            ->field([&#39;sum(age) as all_age&#39;])
//            ->group(&#39;password&#39;)
//            ->having(&#39;all_age > 50&#39;)
//            ->select());

        // 聚合操作count avg sum min max
        dump(Db::name(&#39;user&#39;)->count());
        dump(Db::name(&#39;user&#39;)->sum(&#39;age&#39;));
        dump(Db::name(&#39;user&#39;)->avg(&#39;age&#39;));

        // 获取最近的执行的sql语句
        dump(Db::name(&#39;user&#39;)->getLastSql());
    }


}

Related recommendations:

PHP Methods to connect to mysql-mysqli and PDO


The above is the detailed content of php mysql coherent operation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn