ThinkPHP database operation-data query

1. Single data query

1.1 Single data query

  • The parameter of table in Db:table() must be the complete table name (including prefix)
  • Must specify query conditions
use think\facade\Db;

class DataBaseTest
{
    
    
    public function index()
    {
    
    
//       单数据查询(查询一条数据)查询一条数据
//       使用find函数进行查找,必须指定where查询条件
//       where需要两个参数 field 是表中的字段  op 是对应的值
        $user = Db::table('tp_user')->where('id',27)->find();
        return json($user);
    }
}

1.2 Output the generated SQL statement

Get the SQL statement of the query

use think\facade\Db;

class DataBaseTest
{
    
    
    public function index()
    {
    
    
        $user = Db::table('tp_user')->where('id',27)->find();
//        返回最后一次生成的SQL语句
        $sql =  Db::getLastSql();

        return json($user);
    }
}

1.3 Return a null value or throw an exception

When there is no query result, return a null value or throw an exception

use think\facade\Db;

class DataBaseTest
{
    
    
    public function index()
    {
    
    

        //     当查询不到结果时,返回null
        $user1 = Db::table('tp_user')->where('id',1)->findOrEmpty();
//      当查询不到结果是,则抛出一个异常
        $user2 = Db::table('tp_user')->where('id',1)->findOrFail();
        
        return json($user);
    }
}

Second, the data set query

2.1 Data set query

Get multiple columns of data.

It is not necessary to specify the query conditions.

use think\facade\Db;

class DataBaseTest
{
    
    
    public function index()
    {
    
    
//        可以不指定where条件
        $user = Db::table('tp_user')->select();
        return json($user);
    }
}

2.2 Throw an exception when the data is not found

use think\facade\Db;

class DataBaseTest
{
    
    
    public function index()
    {
    
    
        $user = Db::table('tp_user')->where('id',1111)->selectOrFail();
        return json($user);
    }
}

2.3 Convert the data set object into an array

use think\facade\Db;

class DataBaseTest
{
    
    
    public function index()
    {
    
    
       $user = Db::table('tp_user')->select()->toArray();
       dump($user);
    }
}

2.4 Use the name method, omit the prefix

If the table name prefix is ​​configured in the database configuration, then we can omit the prefix. If the name method is used, the prefix is ​​added and the prefix information is also configured. Then there may be an error

use think\facade\Db;

class DataBaseTest
{
    
    
    public function index()
    {
    
    

       $user = Db::name('user')->select()->toArray();
       dump($user);
    }
}

3. Other queries

3.1 The value method gets the value of a field

Use the Value method to get the value of a field (single data).

The parameter of value is the field name.

use think\facade\Db;

class DataBaseTest
{
    
    
    public function index()
    {
    
    
        $username = Db::name('user')->where('id',27)->value('username');
        dump($username);
    }
}

3.2 The column method queries multiple values ​​of a specified column.

The column parameter is the name of the specified column

use think\facade\Db;

class DataBaseTest
{
    
    
    public function index()
    {
    
    
        $res = Db::name('user')->column('username');
        return json($res);
    }
}

Specify id as index

use think\facade\Db;

class DataBaseTest
{
    
    
    public function index()
    {
    
    
        $res = Db::name('user')->column('username','id');
        return json($res);
    }
}

3.3 Use the chunk method to batch data

In order to avoid reading too much data at once and occupying memory, you can use the chunk method to process the data in batches.

  • The first parameter of the chunk method is how much data is processed each time, and the second parameter is passed in a closure function. The parameter of the function is the result data set.
use think\facade\Db;

class DataBaseTest
{
    
    
    public function index()
    {
    
    
        Db::name('user')->chunk(4,function ($users){
    
    
           foreach ($users as $user) {
    
    
               dump($user);
           }
           echo 'hello world';
        });
    }
}

3.4 Use the cursor to query

With the cursor query function, you can read only one row at a time. When reading again, it can automatically locate to the next line. Can greatly save memory overhead.

use think\facade\Db;

class DataBaseTest
{
    
    
    public function index()
    {
    
    
		$cursor = Db::table('tp_user')->cursor(); 
        foreach($cursor as $user)
        {
    
     
            dump($user); 
        }
    }
}

Four, chain query

4.1 Query rules

  • You can ->call the query method multiple times in succession, which is a chain query.
  • When Db::name('user'), the query object (Query) is returned, and the corresponding method of the database can be connected.
  • Each time the database query method is executed, a query object (Query) is returned. For example, where().
  • As long as it is a database query object, you can always use chain query.
  • Finally, use the find() and select() methods to return the array or data set object.
  • In addition to query methods that can use chain operations, CRUD operations can also be used.

4.2 Repeated query

Every time a database query is used, an instance is generated, causing a waste of resources. We can save the object instance. Then call again.

use think\facade\Db;

class DataBaseTest
{
    
    
    public function index()
    {
    
    
        $userQuery = Db::name('user');
        $resFind = $userQuery->where('id',27)->find();
        $resSelect = $userQuery->select();
    }
}

However, when the same object instance is queried for the second time, the last value will be retained. You can use the removeOption() method to clean up the retained value of the last query

use think\facade\Db;

class DataBaseTest
{
    
    
    public function index()
    {
    
    
  		$userQuery = Db::name('user');
        $resFind = $userQuery->where('id',27)->find();
        $resSelect = $userQuery->select();
        echo Db::getLastSql();
        echo '<br>';
        return json($userQuery->removeOption('where')->select());
        
    }
}

Guess you like

Origin blog.csdn.net/qq_43058685/article/details/112516204