// SELECT * FROM users;
$users = $db->select()->from('users')->execute();
foreach($users as $user) {
var_dump($user);
}
Compile
If you want to see the SQL statement that is generated, you can use the compile() method:
1234
$users = $db->select()->from('users')->where('id', 1);
// SELECT * FROM users WHERE id = '1';
echo $users->compile();
Selecting A Table
1234567
$select = $db->select();
// SELECT * FROM users;
$select->from('users');
// SELECT * FROM users AS u;
$select->from(['users', 'u']);
Selecting Columns
1 2 3 4 5 6 7 8 910111213141516171819
use Qubus\Dbal\DB;
// SELECT id, first_name, last_name, email FROM users;
$db->select('id', 'first_name', 'last_name', 'email')->from('users');
// SELECT first_name AS f_name FROM users;
$db->select(['first_name', 'f_name'])->from('users');
// SELECT first_name AS f_name, email FROM users;
$db->select(['first_name', 'f_name'], 'email')->from('users');
// SELECT COUNT(*) FROM users;
$db->select(DB::fnc('count', '*'))->from('users');
// SELECT COUNT(*) AS num FROM users;
$db->select(DB::fnc('count', '*'))->aliasTo('num')->from('users');
// SELECT COUNT(*) AS num_users FROM users;
$db->select([DB::fnc('count', '*'), 'num_users'])->from('users');
Where Condition
1 2 3 4 5 6 7 8 910111213141516171819202122232425
$select = $db->select()->from('users');
// SELECT * FROM users WHERE id = '1';
$select->where('id', 1);
// SELECT * FROM users WHERE first_name IS NULL;
$select->where('first_name', null);
// SELECT * FROM users WHERE first_name IS NOT NULL;
$select->where('first_name', '!=', null);
// SELECT * FROM users WHERE first_name = 'John' OR email IS NOT NULL;
$select->where('first_name', 'John')->orWhere('email', '!=', null);
// SELECT * FROM users WHERE first_name = 'John' AND email IS NOT NULL;
$select->where('first_name', 'John')->andWhere('email', '!=', null);
// SELECT * FROM users WHERE id IN [1, 3];
$select->where('id', 'in', [1, 3]);
// SELECT * FROM users WHERE id NOT IN [4, 6];
$select->where('id', 'not in', [4, 6]);
// SELECT * FROM users WHERE first_name LIKE 'Jo%';
$select->where('first_name', 'like', 'Jo%');
Joining Tables
1 2 3 4 5 6 7 8 910
$select = $db->select()->from('users');
// SELECT * FROM users JOIN address ON ('users.id' = 'address.user_id');
$select->join('address')->on('users.id', '=', 'address.user_id');
// SELECT * FROM users JOIN address ON ('users.id' = 'address.user_id' AND 'users.email' = 'address.email');
$select->join('address')->on('users.id', '=', 'address.user_id')->andOn('users.email', 'address.email');
// SELECT * FROM users JOIN address ON ('users.id' = 'address.user_id' OR 'users.email' = 'address.email');
$select->join('address')->on('users.id', '=', 'address.user_id')->orOn('users.email', 'address.email');
Ordering and Grouping
1 2 3 4 5 6 7 8 910
$select = $db->select()->from('users');
// SELECT * FROM users ORDER BY 'id';
$select->orderBy('id');
// SELECT * FROM users ORDER BY 'id' DESC;
$select->orderBy('id', 'desc');
// SELECT * FROM users GROUP BY 'id';
$select->groupBy('id');
$select = $db->select('users');
// SELECT * FROM users HAVING id = '3';
$select->having('id', 3);
// SELECT * FROM users HAVING id = '3' AND NOT last_name = 'Johnson';
$select->having('id', 3)->andNotHaving('last_name', 'Johnson');
// SELECT * FROM users HAVING email IS NOT NULL;
$select->having('email', '!=', null);