1. Go to this page and download the library: Download wazsmwazsm/db library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
// SELECT * FROM test WHERE name = 'mike' AND age = 18;
$data = DB::connection('con1')
->table('test')
->where([
'name' => 'mike',
'age' => 18
])
->get();
// SELECT * FROM test WHERE name = 'jack' AND age >= 18;
$data = DB::connection('con1')
->table('test')
->where('name', 'jack')
->where('age', '>=', 18)
->get();
// SELECT * FROM test WHERE name = 'jack' OR name = 'mike';
$data = DB::connection('con1')
->table('test')
->where('name', 'jack')
->orWhere('name', 'mike')
->get();
// SELECT * FROM test WHERE name IS NOT null;
$data = DB::connection('con1')
->table('test')
->whereNotNull('name')
->get();
// SELECT * FROM test WHERE age BETWEEN 18 AND 30;
$data = DB::connection('con1')
->table('test')
->whereBetween('age', 18, 30)
->get();
// SELECT * FROM test WHERE age IN (18, 19, 20);
$data = DB::connection('con1')
->table('test')
->whereIn('age', [18, 19, 20])
->get();
// SELECT * FROM user WHERE EXISTS ( SELECT * FROM user_group WHERE id = 3 ) AND g_id = 3;
$data = DB::connection('con1')
->table('user')
->whereExists(function($query) {
$query->table('user_group')->where('id', 3);
})
->where('g_id', 3)
->get();
// SELECT * FROM user WHERE g_id IN (SELECT id FROM user _group);
$data = DB::connection('con1')
->table('user')
->whereInSub('g_id', function($query) {
$query->table('user_group')->select('id');
})
->get();
// SELECT * FROM user WHERE (id < 50 OR username IS NOT NULL) AND sort_num = 20;
$data = DB::connection('con1')
->table('user')
->whereBrackets(function($query) {
$query->where('id', '<', 50)
->orWhereNotNull('username');
})
->where('sort_num', 20)
->get();
// SELECT * FROM user WHERE sort_num = 20 OR (id < 10 AND id > 5);
$data = DB::connection('con1')
->table('user')
->where('sort_num', 20)
->orWhereBrackets(function($query) {
$query->where('id', '<', 10)
->where('id', '>', 5);
})
->get();
// SELECT id, username, email FROM ( SELECT * FROM user WHERE id < 20 ) AS tb;
$data = DB::connection('con1')
->select('id', 'username', 'email')
->fromSub(function($query) {
$query->table('user')->where('id', '<', '20');
})
->get();
// SELECT sort_num, COUNT(sort_num) FROM user GROUP BY sort_num;
$data = DB::connection('con1')
->table('user')
->select('sort_num', 'COUNT(sort_num)')
->groupBy('sort_num')
->get();
// SELECT sort_num, COUNT(sort_num) FROM user GROUP BY sort_num HAVING COUNT(sort_num) < 20;
$data = DB::connection('con1')
->table('user')
->select('sort_num', 'COUNT(sort_num)')
->groupBy('sort_num')
->having('COUNT(sort_num)', '<', 20)
->get();
// SELECT sort_num, COUNT(sort_num) FROM user GROUP BY sort_num HAVING COUNT(sort_num) < 20;
$data = DB::connection('con1')
->table('user')
->select('sort_num', 'COUNT(sort_num)')
->groupBy('sort_num')
->havingRaw('COUNT(sort_num) < 20')
->get();
// SELECT * FROM user ORDER BY sort_num DESC, id ASC;
$data = DB::connection('con1')
->table('user')
->orderBy('sort_num', 'DESC')
->orderBy('id', 'ASC')
->get();
// SELECT * FROM user LIMIT 10 OFFSET 3
$data = DB::connection('con1')
->table('user')
->limit(3, 10)
->get();