zoukankan      html  css  js  c++  java
  • thinkphp中的查询语句

    <?php
    namespace AdminController;
    use ThinkController;
    class MainController extends Controller
    {
        public function showList()
        {
            echo "大苹果商城";
        }
        
        public function test()
        {
            //数据访问
            //造模型对象
            //$nation = D("Nation");//连接数据库中的Nation表
            
            //查询
            //$a = $nation->select();//查询所有,返回关联数组,也是二维数组。
            //$a = $nation->select("n001,n002,n003");//通过主键值查,注意写法。
            
            //$a = $nation->find();//查一条数据,返回一维数组。
            //$a = $nation->find("n001");//按照条件查询
            
            //$a = $nation->where("name='汉族'or name='回族'")->select();//where()只能写查询条件。select才会输出,select返回的是二维数组。这种操作称为连贯操作
            
            //$a = $nation->table("info")->select();//table切换其它表格查询表格信息。上面虽然是链接的Nation表,这里切换到info表格。
            
            //$a = $nation->field("code")->select();//查询指定字段,这里查询的是code列,多个字段中间用,分隔。
            
            //$a = $nation->order("code desc")->select();//code按照降序排列
            
            //$a = $nation->limit(3)->select();//查询前3条数据,limit分页查询。
            //$a = $nation->limit(3,4)->select();//跳过3条显示4条
            
            //$a = $nation->page(2,3)->select();//page中第一个参数是第几页,第二个参数是几条数据。这里是取第2页的3条数据。
            
            //$a = $nation->table("car")->field("Brand,avg(price)")->group("brand")->select();//分组查询
            
            //$a = $nation->table("car")->field("Brand,avg(price)")->group("brand")->having("avg(price)>50")->select();//having条件查询
            
            //$a = $nation->field("Info.code,Info.name as 'name',nation.name as '民族'")->join("Info on Nation.code=Info.Nation")->select();//用join连接的时候前面的field的列要加上别名,不然会出问题。
            
            //$a = $nation->table("car")->distinct(true)->field("brand")->select();//distinct去重
            
            //$a = $nation->where("code='n003'")->getField("name");//只能写列名,获取某一列的值。
            
            //$a = $nation->count();//也可以放在连贯操作的最后用,求出数据量。
            
            //$a = $nation->table("car")->max("price");
            
            //使用原生的查询方法
            //$sql = "select * from nation";
            //$a = $nation->query($sql);//查询语句还是按照原来的调用方法。
            
            //$sql = "update nation set name='矮人族' where code='n001'";
            //$a = $nation->execute($sql);//增、删、改语句用execute调用。
            
            //var_dump($a);
        }
    }
    
    ?>
  • 相关阅读:
    LeetCode 1122. Relative Sort Array (数组的相对排序)
    LeetCode 46. Permutations (全排列)
    LeetCode 47. Permutations II (全排列 II)
    LeetCode 77. Combinations (组合)
    LeetCode 1005. Maximize Sum Of Array After K Negations (K 次取反后最大化的数组和)
    LeetCode 922. Sort Array By Parity II (按奇偶排序数组 II)
    LeetCode 1219. Path with Maximum Gold (黄金矿工)
    LeetCode 1029. Two City Scheduling (两地调度)
    LeetCode 392. Is Subsequence (判断子序列)
    写程序判断系统是大端序还是小端序
  • 原文地址:https://www.cnblogs.com/xiaofox0018/p/6219415.html
Copyright © 2011-2022 走看看