Select statements - AwesomeNine/Database GitHub Wiki

Examples

$table = new TheLeague\Database\Database::table( 'users' )

// select * from wp_users
$table->get();

// select distinct * from wp_users
$table->distinct()->get();

// select SQL_CALC_FOUND_ROWS * from wp_users
$table->found_rows()->get();

Specify columns for Select statement

// select id from wp_users
$table->select( 'id' )->get();

// select id, user_login from wp_users
$table->select( 'id, user_login' )->get();

// select id, user_login from wp_users
$table->select( array( 'id', 'user_login' ) )->get();

// select id, user_login as username from wp_users
$table->select( array( 'id', 'user_login as username' ) )->get();

// select id, user_login as username from wp_users
$table->select( array(
	'id',
	'user_login' => 'username'
) )->get();

Select statement with count, sum, avg

// select count(*) from wp_users
$table->selectCount()->get();

// select count( id ) as count from wp_users
$table->selectCount( 'id', 'count' )->get();

// select sum( id ) as total from wp_users
$table->selectSum( 'id', 'total' )->get();

// select avg( id ) as average from wp_users
$table->selectAvg( 'id', 'average' )->get();

Select a single row

// select * from wp_users WHERE user_email = '[email protected]' LIMIT 0, 1;
$table->where( 'user_email', '[email protected]' )->one();

To retrieve a value

$email = $table->select( 'user_email' )->where( 'ID', 123 )->getVar();