【问题标题】:how using SQL IN operator in find method of cakephp ORM如何在 cakephp ORM 的 find 方法中使用 SQL IN 运算符
【发布时间】:2012-03-15 17:59:06
【问题描述】:

我是 cakephp 的初学者,我想在 find 方法中使用 SQL IN 运算符,我有单词表。
我的代码是:

$this->Word->find('Word.wordid in (83,82)');

,这段代码创建了这个查询:

SELECT `Userword`.`userwordid`, `Userword`.`userid`, `Userword`.`wordid`, 
`Userword`.`date`, `Userword`.`levelid` FROM `userwords` AS `Userword` WHERE 
`Userword`.`wordid` = (82) 

但我需要这个查询

SELECT `Userword`.`userwordid`, `Userword`.`userid`, `Userword`.`wordid`, 
Userword`.`date`, `Userword`.`levelid` FROM `userwords` AS `Userword` WHERE 
`Userword`.`wordid` IN (83,82)

如何得到这样的查询(使用 IN 运算符)
谢谢。

【问题讨论】:

    标签: cakephp orm find in-operator


    【解决方案1】:

    你需要让 cake 来处理它 - 只需将它作为一个字符串使用(但确保它是一个数组):

    $arrayOfIds = [1, 5, ...];
    $this->Word->find('all', array(
        'conditions' => array('Word.wordid' => $arrayOfIds)
    ));
    

    【讨论】:

    • 请注意,这仅对 CakePHP 'Word.wordid IN' => $arrayOfIds
    • 无法构建查询,例如SELECT User.email, User.id` FROM users AS User WHERE User.specialization_id = '2 ,19'`
    • $allUsers = $this->Query->User->find('all', array( 'fields' => array('User.email', 'User.email'), 'conditions' => array('User.specialization_id' => $specializationId) ));
    • 你没有使用数组!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-06
    相关资源
    最近更新 更多