博客列表 >数据库查询10条-2018年05月25日01:22

数据库查询10条-2018年05月25日01:22

植树青年小江同志的博客
植树青年小江同志的博客原创
2018年05月25日 01:44:39697浏览

table(),field(),order(), where(), limit(),使用

实例

<?php
namespace app\index\controller;

use think\db;


class Query
{
    // table(),field(),order(), where(), limit(),使用

    public function select()
    {
        $res = Db::table('staff')
                ->field(['name' => '姓名', 'age' => '年龄', 'salary' => '工资'])
                ->where('staff_id', '>' , 3)
                ->order('salary DECS')
                ->limit(7)
                ->select();
                dump($res);
                
    }
}

运行实例 »

点击 "运行实例" 按钮查看在线实例


返回结果:

array(7) {
 [0] => array(3) {
   ["姓名"] => string(6) "杨康"
   ["年龄"] => int(28)
   ["工资"] => int(4000)
 }
 [1] => array(3) {
   ["姓名"] => string(9) "欧阳峰"
   ["年龄"] => int(30)
   ["工资"] => int(4000)
 }
 [2] => array(3) {
   ["姓名"] => string(6) "关羽"
   ["年龄"] => int(53)
   ["工资"] => int(5000)
 }
 [3] => array(3) {
   ["姓名"] => string(12) "还珠格格"
   ["年龄"] => int(18)
   ["工资"] => int(5000)
 }
 [4] => array(3) {
   ["姓名"] => string(9) "武大郎"
   ["年龄"] => int(25)
   ["工资"] => int(5000)
 }
 [5] => array(3) {
   ["姓名"] => string(6) "武松"
   ["年龄"] => int(25)
   ["工资"] => int(5000)
 }
 [6] => array(3) {
   ["姓名"] => string(6) "刘备"
   ["年龄"] => int(58)
   ["工资"] => int(5000)
 }
}

insert(),数据打包data()

实例

 public function insert()
    { 
        $data = [
            'name' => '李小璐',
            'age' =>'33',
            'sex' => 1,
            'salary' => '1000000',

        ];


        $res = Db::table('staff')
                ->data($data)
                ->insertGetId();

        

        return $res ? '添加成功' . 'id=' . $res  : '添加失败';
    }

运行实例 »

点击 "运行实例" 按钮查看在线实例

insertAll()

实例

public function insertAll()
    {
        $data = [
            ['name' => '贾乃亮', 'age' => 30, 'sex' => 0, 'salary' => 1],
            ['name' => 'PGOne', 'age' => 28, 'sex' => 0, 'salary' => 999],

        ];

        $res = Db::table('staff')
                ->data($data)
                ->insertAll();

                return $res ? '添加成功' . $res : '添加失败';
    }

运行实例 »

点击 "运行实例" 按钮查看在线实例

update()

实例

public function update()
    {
        $res= Db::table('staff')
                ->where('name', '=', 'PGOne')
                ->data(['salary'=>'0'])
                ->update();

        return $res ? '添加成功' . $res : '添加失败';
    }

运行实例 »

点击 "运行实例" 按钮查看在线实例

delete()

实例

 public function delete()
    {
        $res = Db::table('staff')
                ->where('staff_id', '>' , 31)
                ->delete();

        return $res ? '添加成功' . $res : '添加失败';
    }

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

public function insertGetId()
    { 
        $data = [
            'name' => '李小璐',
            'age' =>'33',
            'sex' => 1,
            'salary' => '1000000',

        ];


        $res = Db::table('staff')->insertGetId($data);

        $id = Db::getLastInsID();

        return $res ? '添加成功' . 'id=' . $id  : '添加失败';
    }

运行实例 »

点击 "运行实例" 按钮查看在线实例

获取自增主键

实例

public function insertGetId()
    { 
        $data = [
            'name' => '李小璐',
            'age' =>'33',
            'sex' => 1,
            'salary' => '1000000',

        ];


        $res = Db::table('staff')->insertGetId($data);

        $id = Db::getLastInsID();

        return $res ? '添加成功' . 'id=' . $id  : '添加失败';
    }

运行实例 »

点击 "运行实例" 按钮查看在线实例


声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议