敏捷工具包ATK4 DSQL中SQL'BETWEEN'和'IN'的等价物

时间:2011-12-17 02:40:50

标签: mysql sql atk4 agiletoolkit

我需要ATK4 DSQL的帮助,我只想检索一个特定的 我的Employee Model Table类中的一组行,使用动态SQL。

我可以在WHERE语句中生成SQL BETWEEN或IN子句 这样它会“某种程度上”看起来像这些结构:

SQL 'IN'

SELECT id,fnm,lnm,bp FROM emp WHERE id IN(1,3,5,7,9,11);

$model=$this->add('Model_Employee');
$set=array(1,3,5,7,9,11);
$list=$mode->dsql()
   ->field('id')
   ->field('fnm') // first name
   ->field('lnm') // last name
   ->field('bp') // basic pay
   ->where('id in (%l)',$set) // only 1,3,5,7,9 & 11
   ->do_getAll();

SQL 'BETWEEN'

SELECT id,fnm,lnm,bp FROM emp WHERE bp BETWEEN 3000和5000;

$model=$this->add('Model_Employee');
$min=3000;$max=5000;
$range=$mode->dsql()
   ->field('id')
   ->field('fnm') // first name
   ->field('lnm') // last name
   ->field('bp') // basic pay
   ->where('bp between %i and %i',$min,$max)
   ->do_getAll();

谢谢!

1 个答案:

答案 0 :(得分:1)

针对当前版本的DSQL进行了更新http://git.io/dsql

来自documentation

$query -> where('time', $query->expr(
    'between [] and []',
    [$from, $to]
));

旧答案:在Agile Toolkit中高达4.1。*“where”非常局限于表达式。你需要这样写:

->where('bp between '.
    $this->api->db->escape($min).' and '.
    $this->api->db->escape($max));