Return Values

成功返回 Object , 失败时返回 false

Examples

Example #1 select ... right join ... having

<?php
// [SQL] SELECT * FROM `test` a RIGHT JOIN `image` b ON a.id = b.id HAVING b.id > ? [BVALUES] 100

$select = new Asf_Db_QueryBuilder_Select();
$select
->cols()
->from('test', 'a')
->rightJoin('image', 'b')
->on('a.id', 'b.id')
->having('b.id', 100, '>');

Example #2 select ... where ... orWhere ... whereIn

<?php
// [SQL] SELECT * FROM `test` WHERE `pid` IN (?,?) OR `cid` IN (?,?) [BVALUES] 100 , 200 , 10 , 20

$select = new Asf_Db_QueryBuilder_Select();
$select
->cols()
->from('test')
->whereIn('pid', array('100', 200))
->orWhereIn('cid', array('10', 20));

Example #3 select count(*) ... where

<?php
// [SQL] SELECT count(*) cnt FROM `test1` WHERE `s` = ? [BVALUES] 0

$select = new Asf_Db_QueryBuilder_Select();
$select->count('*', 'cnt')->from('test1')->where('s', 0);

Example #4 select ... limit

<?php
// [SQL] SELECT * FROM `test` LIMIT 0, 10

$select = new Asf_Db_QueryBuilder_Select();
$select->cols()->from('test')->limit(0, 10);

Example #5 select ... left join ... group by ... order by

<?php
// [SQL] SELECT * FROM `test` a LEFT JOIN `image` b INNER JOIN `art` b ON a.id = b.id GROUP BY a.id ORDER BY a.id ASC

$select = new Asf_Db_QueryBuilder_Select();
$select
->cols()
->from('test', 'a')
->leftJoin('image', 'b')
->InnerJoin('art', 'b')
->on('a.id', 'b.id')
->groupBy('a.id')
->orderBy('a.id', 'ASC');

Example #6 select ... max, min, sum, count, distinct

<?php
// [SQL] SELECT max(`username`)  , min(`username`)  , sum(`username`)  , count(`username`)  , distinct(`username`) FROM `test` a

$select = new Asf_Db_QueryBuilder_Select();
$select
->max('username')
->cm(',')
->min('username')
->cm(',')
->sum('username')
->cm(',')
->count('username')
->cm(',')
->distinct('username')
->from('test', 'a');

Example #7 union all

<?php
// [SQL] SELECT * FROM `test1` WHERE `id` > ? AND `status` = ? UNION ALL SELECT * FROM `test1` WHERE `id` < ? [BVALUES] 100 , 2 , 50

$select = new Asf_Db_QueryBuilder_Select();
$select->cols()->from('test1')->where('id', '100', '>')->where('status', 2)
->unionAll()
select()->cols()->from('test1')->where('id', '50', '<');

Example #8 select ... where

<?php
// [SQL] SELECT `a`, `b`, `c`, `d` FROM `test` WHERE `k` = ? [BVALUES] 1

$select = new Asf_Db_QueryBuilder_Select();
$select->cm('a, b, c, d')->from('test')->where('k', 1);
var_dump($select->show());

Example #9 select ... where ... order by ... limit

<?php
// SELECT * FROM `news` WHERE `status` = ? ORDER BY `id` DESC  limit 0, 10

$limit = 'limit 0, 10';

Asf_Db::init(...)
Asf_Db::QBS() 
  ->cols()->from('news')
  ->where('status', 0)
  ->orderBy('id', 'DESC') 
  ->cm($limit)  
  ->exec();

Example #10 update ... where

<?php
// UPDATE `news` SET `status` = ? WHERE `id` = ?

Asf_Db::init(...)
Asf_Db::QBU()
  ->table('news')
  ->set('status', 1)
  ->where('id', $id)
  ->exec();

Example #11 select a, b, c ... where ... order by ... limit

<?php
// SELECT `a`, `b`, `c` FROM `cart` WHERE `status` = ? ORDER BY `id` DESC LIMIT 50

Asf_Db::init(...)
Asf_Db::QBS()
->cols(array('a', 'b', 'c'))
->from('cart')
->where('status', 0)
->orderBy('id', 'DESC')
->limit(50)
->exec();

Example #12 select count(`id`) cnt ... where

<?php
// SELECT count(`id`) cnt FROM `cart` WHERE `status` = ?

Asf_Db::init(...)
$data = Asf_Db::QBS()
->count('id', 'cnt')
->from('cart')
->where('status', 0)
->exec(1);

echo $data['cnt'];

Example #13 select ... like

<?php
// [SQL] SELECT `a`, `b`, `c`, `d` FROM `test` WHERE `k` like '%?%' [BVALUES] 1

$select = new Asf_Db_QueryBuilder_Select();
$select->cols(array('a', 'b', 'c', 'd'))->from('test')->like('k', 1);

Example #14 select ... wheres

<?php
// [SQL] SELECT * FROM `test` WHERE `d` = ? AND `g` = ? ORDER BY `id` DESC [BVALUES] 1 , hhh

$select = Asf_Db::qbs();
$select->cols()->from('test')->wheres(array('d' => 1, 'g' => 'hhh'))->orderBy('id');

Example #15 select ... cols(string)

If cols(string) Fields will not be escaped. If cols(array) Fields will be escaped(`字段名`)

<?php
// string(55) "[SQL] SELECT distinct(`model`)  ,id as ids FROM `test1`"

$select = Asf_Db::qbs();
$select->distinct('model')->cols(',id as ids')->from('test1');